当前位置:首页 > VUE

vue前端实现分页查询

2026-01-21 13:10:42VUE

vue前端实现分页查询

实现分页查询的基本思路

Vue前端实现分页查询通常需要结合后端API,通过传递页码(page)和每页条数(pageSize)参数获取分页数据。前端需要处理页码切换、数据渲染和分页器交互逻辑。

核心代码结构

<template>
  <div>
    <table>
      <tr v-for="item in tableData" :key="item.id">
        <!-- 表格内容渲染 -->
      </tr>
    </table>

    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>{{ currentPage }} / {{ totalPage }}</span>
      <button @click="nextPage" :disabled="currentPage === totalPage">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],   // 当前页数据
      currentPage: 1,  // 当前页码
      pageSize: 10,    // 每页条数
      total: 0         // 总数据量
    }
  },
  computed: {
    totalPage() {
      return Math.ceil(this.total / this.pageSize)
    }
  },
  methods: {
    fetchData() {
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize
      }

      axios.get('/api/data', { params })
        .then(res => {
          this.tableData = res.data.list
          this.total = res.data.total
        })
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
        this.fetchData()
      }
    },
    nextPage() {
      if (this.currentPage < this.totalPage) {
        this.currentPage++
        this.fetchData()
      }
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

使用Element UI的分页组件

如果使用Element UI,可以简化分页逻辑:

<template>
  <div>
    <el-table :data="tableData">
      <!-- 表格列定义 -->
    </el-table>

    <el-pagination
      @current-change="handlePageChange"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
      layout="prev, pager, next">
    </el-pagination>
  </div>
</template>

<script>
export default {
  methods: {
    handlePageChange(page) {
      this.currentPage = page
      this.fetchData()
    }
  }
}
</script>

分页查询优化建议

  1. 防抖处理:频繁切换页码时添加防抖,避免短时间内多次请求
  2. 缓存策略:已访问过的页码数据可做本地缓存
  3. 滚动加载:移动端可考虑无限滚动模式替代传统分页
  4. URL同步:将当前页码同步到URL参数,便于分享和刷新保持状态

完整示例代码

<template>
  <div>
    <input v-model="searchQuery" placeholder="搜索..." @input="handleSearch"/>

    <table>
      <tr v-for="item in filteredData" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.value }}</td>
      </tr>
    </table>

    <div class="pagination">
      <button 
        v-for="page in pageNumbers" 
        :key="page"
        @click="goToPage(page)"
        :class="{ active: page === currentPage }">
        {{ page }}
      </button>
    </div>
  </div>
</template>

<script>
import _ from 'lodash'

export default {
  data() {
    return {
      allData: [],
      filteredData: [],
      searchQuery: '',
      currentPage: 1,
      pageSize: 5,
      totalItems: 0
    }
  },
  computed: {
    totalPages() {
      return Math.ceil(this.totalItems / this.pageSize)
    },
    pageNumbers() {
      const range = []
      const maxVisible = 5
      let start = Math.max(1, this.currentPage - Math.floor(maxVisible/2))
      let end = Math.min(this.totalPages, start + maxVisible - 1)

      if(end - start < maxVisible - 1) {
        start = Math.max(1, end - maxVisible + 1)
      }

      for(let i = start; i <= end; i++) {
        range.push(i)
      }

      return range
    }
  },
  methods: {
    fetchData: _.debounce(function() {
      axios.get('/api/data', {
        params: {
          page: this.currentPage,
          pageSize: this.pageSize,
          query: this.searchQuery
        }
      }).then(response => {
        this.filteredData = response.data.items
        this.totalItems = response.data.total
      })
    }, 300),

    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },

    goToPage(page) {
      if(page !== this.currentPage) {
        this.currentPage = page
        this.fetchData()
      }
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

<style>
.pagination button {
  margin: 0 5px;
}
.pagination button.active {
  font-weight: bold;
  color: blue;
}
</style>

vue前端实现分页查询

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

相关文章

vue   实现单选

vue 实现单选

Vue 实现单选功能 在 Vue 中实现单选功能可以通过多种方式,以下是几种常见的方法: 使用原生 <input type="radio"> 绑定 v-model 通过 v-model…

vue菜单实现

vue菜单实现

Vue 菜单实现方法 使用 Element UI 的菜单组件 Element UI 提供了现成的菜单组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用 el-menu 相关组件。…

vue 实现递归

vue 实现递归

Vue 实现递归组件的方法 在 Vue 中实现递归组件通常用于处理树形结构数据,例如菜单、评论列表或文件目录。以下是实现递归组件的关键步骤: 定义递归组件 递归组件需要设置 name 属性,以便在…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,…

vue 实现遮罩

vue 实现遮罩

Vue 实现遮罩层的方法 使用固定定位和透明背景 在Vue中实现遮罩层可以通过CSS固定定位结合透明背景色完成。创建一个全屏遮罩组件,利用position: fixed覆盖整个视窗。 <tem…