当前位置:首页 > VUE

vue实现查询分页

2026-01-22 00:22:12VUE

Vue 实现查询分页的方法

基本分页实现

在Vue中实现分页通常需要结合后端API,前端处理分页逻辑和UI展示。以下是一个基于Element UI的分页组件示例:

<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: {
    fetchData() {
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      // 调用API获取数据
      api.getList(params).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

查询与分页结合

实现带查询条件的分页功能,需要在请求参数中加入查询条件:

<template>
  <div>
    <el-form :inline="true" @submit.native.prevent="handleSearch">
      <el-form-item>
        <el-input v-model="searchQuery" placeholder="请输入关键词"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格和分页组件 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchQuery: '',
      // 其他分页数据...
    }
  },
  methods: {
    handleSearch() {
      this.currentPage = 1  // 重置到第一页
      this.fetchData()
    },
    fetchData() {
      const params = {
        page: this.currentPage,
        size: this.pageSize,
        keyword: this.searchQuery
      }
      // 调用API
    }
  }
}
</script>

使用Vuex管理分页状态

对于复杂应用,可以使用Vuex集中管理分页状态:

vue实现查询分页

// store/modules/list.js
const state = {
  list: [],
  pagination: {
    page: 1,
    size: 10,
    total: 0
  },
  query: ''
}

const actions = {
  fetchList({ commit, state }) {
    return api.getList({
      page: state.pagination.page,
      size: state.pagination.size,
      keyword: state.query
    }).then(res => {
      commit('SET_LIST', res.data.list)
      commit('SET_TOTAL', res.data.total)
    })
  }
}

// 组件中
import { mapState, mapActions } from 'vuex'

export default {
  computed: {
    ...mapState('list', ['list', 'pagination', 'query'])
  },
  methods: {
    ...mapActions('list', ['fetchList']),
    handleSearch() {
      this.$store.commit('list/RESET_PAGE')
      this.fetchList()
    }
  }
}

无UI库的纯Vue分页实现

如果不使用UI库,可以手动实现分页组件:

<template>
  <div class="pagination">
    <button 
      @click="changePage(currentPage - 1)"
      :disabled="currentPage === 1">
      上一页
    </button>

    <button
      v-for="page in pages"
      :key="page"
      @click="changePage(page)"
      :class="{ active: currentPage === page }">
      {{ page }}
    </button>

    <button
      @click="changePage(currentPage + 1)"
      :disabled="currentPage === totalPages">
      下一页
    </button>
  </div>
</template>

<script>
export default {
  props: {
    currentPage: Number,
    total: Number,
    perPage: Number
  },
  computed: {
    totalPages() {
      return Math.ceil(this.total / this.perPage)
    },
    pages() {
      const range = []
      const start = Math.max(1, this.currentPage - 2)
      const end = Math.min(this.totalPages, this.currentPage + 2)

      for (let i = start; i <= end; i++) {
        range.push(i)
      }
      return range
    }
  },
  methods: {
    changePage(page) {
      if (page >= 1 && page <= this.totalPages) {
        this.$emit('page-changed', page)
      }
    }
  }
}
</script>

分页性能优化

对于大数据量分页,可以考虑以下优化策略:

vue实现查询分页

实现前端缓存,避免重复请求相同页面的数据

// 在data中增加缓存对象
data() {
  return {
    pageCache: {}
  }
}

// 修改fetchData方法
fetchData() {
  const cacheKey = `${this.currentPage}-${this.pageSize}-${this.searchQuery}`
  if (this.pageCache[cacheKey]) {
    this.tableData = this.pageCache[cacheKey].list
    this.total = this.pageCache[cacheKey].total
    return
  }

  // 正常请求API
  api.getList(params).then(res => {
    this.tableData = res.data.list
    this.total = res.data.total
    this.pageCache[cacheKey] = {
      list: res.data.list,
      total: res.data.total
    }
  })
}

使用防抖处理频繁的分页请求

import { debounce } from 'lodash'

methods: {
  fetchData: debounce(function() {
    // 实际获取数据的逻辑
  }, 300)
}

以上方法提供了不同场景下Vue实现查询分页的解决方案,可根据具体项目需求选择适合的方式。

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

相关文章

vue computed实现

vue computed实现

计算属性的基本用法 在Vue中,计算属性通过computed选项定义,用于声明依赖于其他属性的动态值。计算属性基于它们的依赖进行缓存,只有依赖发生变化时才会重新计算。 new Vue({ dat…

vue实现flbook

vue实现flbook

Vue 实现类似 Flbook 的翻页效果 要实现类似 Flbook 的翻页效果,可以使用 Vue 结合 CSS 动画和 JavaScript 事件处理。以下是实现方法: 安装依赖 需要安装 vu…

vue实现分步

vue实现分步

Vue 实现分步功能 在 Vue 中实现分步功能可以通过多种方式完成,以下是几种常见的方法: 使用动态组件 通过动态组件切换不同的步骤内容,结合 v-if 或 component 标签实现分步逻辑。…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装…

js 实现vue

js 实现vue

实现 Vue 的核心功能 在 JavaScript 中实现 Vue 的核心功能需要模拟数据绑定、虚拟 DOM 和响应式系统。以下是一个简化版的实现思路: 响应式系统 通过 Object.defin…

vue实现肢体识别

vue实现肢体识别

Vue 中实现肢体识别的方法 在 Vue 项目中实现肢体识别通常需要结合第三方库或 API,例如 TensorFlow.js、MediaPipe 或 OpenCV。以下是几种常见的实现方式: 使用…