当前位置:首页 > VUE

vue分页查询怎么实现

2026-01-23 06:04:18VUE

vue分页查询实现方法

使用Element UI的分页组件

安装Element UI库并引入Pagination组件:

npm install element-ui

在Vue文件中引入并使用:

<template>
  <div>
    <el-table :data="tableData">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      }).then(response => {
        this.tableData = response.data.list
        this.total = response.data.total
      })
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

自定义分页实现

不使用UI库时,可以手动实现分页功能:

<template>
  <div>
    <table>
      <!-- 表格内容 -->
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === totalPages">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      currentPage: 1,
      pageSize: 10,
      totalItems: 0
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      return this.data.slice(start, end)
    }
  },
  methods: {
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPages) {
        this.currentPage++
      }
    },
    fetchData() {
      // 获取数据逻辑
    }
  }
}
</script>

后端API配合

确保后端API支持分页参数:

// Express示例
app.get('/api/data', (req, res) => {
  const page = parseInt(req.query.page) || 1
  const size = parseInt(req.query.size) || 10
  const offset = (page - 1) * size

  // 查询数据库
  db.query('SELECT * FROM table LIMIT ? OFFSET ?', [size, offset])
    .then(results => {
      db.query('SELECT COUNT(*) as total FROM table')
        .then(countResult => {
          res.json({
            list: results,
            total: countResult[0].total,
            page,
            size
          })
        })
    })
})

分页优化建议

使用防抖技术减少频繁请求:

methods: {
  fetchData: _.debounce(function() {
    // 请求逻辑
  }, 300)
}

添加加载状态提升用户体验:

<template>
  <el-pagination v-loading="loading" ... />
</template>

<script>
export default {
  data() {
    return {
      loading: false
    }
  },
  methods: {
    fetchData() {
      this.loading = true
      axios.get(...).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>

分页样式定制

通过CSS自定义分页样式:

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.pagination button {
  margin: 0 10px;
  padding: 5px 10px;
  background: #409EFF;
  color: white;
  border: none;
  border-radius: 3px;
}

.pagination button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

vue分页查询怎么实现

标签: 分页vue
分享给朋友:

相关文章

vue实现多选题

vue实现多选题

Vue实现多选题的方法 使用Vue实现多选题功能,可以通过v-model绑定数组、动态渲染选项、以及处理选中状态来实现。以下是一个完整的实现示例: 基础实现代码 <template>…

vue实现递归

vue实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据(如菜单、评论列表、文件目录等)。以下是具体实现方法: 定义递归组件 通过 name 选项让组件可以递归调用自身:…

vue实现主题

vue实现主题

Vue 实现主题的方法 使用 CSS 变量动态切换主题 定义全局 CSS 变量,通过修改这些变量实现主题切换。在 Vue 的根组件或全局样式中定义变量: :root { --primary-c…

vue交互实现

vue交互实现

Vue 交互实现方法 Vue.js 提供了多种方式实现用户交互,包括事件处理、表单绑定、动态渲染等。以下是常见的交互实现方法: 事件处理 通过 v-on 或 @ 指令绑定事件,触发方法或直接执行表达…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖…

vue实现素材

vue实现素材

Vue 实现素材的方法 使用 Vue 组件管理素材 在 Vue 项目中,可以通过组件化的方式管理素材。创建一个专门的组件来加载和显示素材,例如图片、视频或音频文件。组件可以接收素材的路径或 URL 作…