当前位置:首页 > VUE

vue实现页面分页

2026-01-14 05:45:56VUE

Vue实现页面分页的方法

使用Element UI的分页组件

Element UI提供了现成的分页组件el-pagination,适合快速集成。以下是一个示例代码:

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

<script>
export default {
  data() {
    return {
      tableData: [], // 所有数据
      currentPageData: [], // 当前页数据
      currentPage: 1,
      pageSize: 10,
      total: 0
    }
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.updateCurrentPageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.updateCurrentPageData()
    },
    updateCurrentPageData() {
      const start = (this.currentPage - 1) * this.pageSize
      const end = start + this.pageSize
      this.currentPageData = this.tableData.slice(start, end)
    }
  },
  created() {
    // 模拟获取数据
    this.tableData = Array.from({length: 100}, (_, i) => ({id: i, name: `Item ${i}`}))
    this.total = this.tableData.length
    this.updateCurrentPageData()
  }
}
</script>

自定义分页实现

如果不使用UI框架,可以手动实现分页功能:

<template>
  <div>
    <table>
      <tr v-for="item in paginatedData" :key="item.id">
        <td>{{ item.name }}</td>
      </tr>
    </table>
    <div class="pagination">
      <button @click="prevPage" :disabled="currentPage === 1">上一页</button>
      <span>第 {{ currentPage }} 页</span>
      <button @click="nextPage" :disabled="currentPage === pageCount">下一页</button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [], // 所有数据
      currentPage: 1,
      itemsPerPage: 10
    }
  },
  computed: {
    pageCount() {
      return Math.ceil(this.items.length / this.itemsPerPage)
    },
    paginatedData() {
      const start = (this.currentPage - 1) * this.itemsPerPage
      const end = start + this.itemsPerPage
      return this.items.slice(start, end)
    }
  },
  methods: {
    nextPage() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++
      }
    },
    prevPage() {
      if (this.currentPage > 1) {
        this.currentPage--
      }
    }
  },
  created() {
    // 模拟数据
    this.items = Array.from({length: 100}, (_, i) => ({id: i, name: `Item ${i}`}))
  }
}
</script>

结合后端API的分页

实际项目中通常需要与后端API交互,实现真正的分页查询:

<template>
  <div>
    <el-table :data="tableData" loading-text="加载中..." v-loading="loading">
      <!-- 表格列定义 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      loading: false
    }
  },
  methods: {
    fetchData() {
      this.loading = true
      // 替换为实际的API调用
      axios.get('/api/data', {
        params: {
          page: this.currentPage,
          size: this.pageSize
        }
      }).then(response => {
        this.tableData = response.data.items
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    }
  },
  created() {
    this.fetchData()
  }
}
</script>

使用Vuex管理分页状态

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

// store/modules/data.js
const state = {
  items: [],
  pagination: {
    currentPage: 1,
    pageSize: 10,
    total: 0
  }
}

const mutations = {
  SET_ITEMS(state, payload) {
    state.items = payload.items
    state.pagination.total = payload.total
  },
  SET_PAGE(state, page) {
    state.pagination.currentPage = page
  }
}

const actions = {
  async fetchItems({ commit, state }) {
    const { currentPage, pageSize } = state.pagination
    const response = await api.getItems(currentPage, pageSize)
    commit('SET_ITEMS', {
      items: response.data.items,
      total: response.data.total
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

在组件中使用:

<template>
  <div>
    <el-table :data="items">
      <!-- 表格内容 -->
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-size="pagination.pageSize"
      :total="pagination.total">
    </el-pagination>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  computed: {
    ...mapState('data', ['items', 'pagination'])
  },
  methods: {
    ...mapActions('data', ['fetchItems']),
    handleCurrentChange(page) {
      this.$store.commit('data/SET_PAGE', page)
      this.fetchItems()
    }
  },
  created() {
    this.fetchItems()
  }
}
</script>

vue实现页面分页

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

相关文章

vue router实现分页

vue router实现分页

Vue Router 实现分页的方法 在 Vue.js 中,可以通过 Vue Router 实现分页功能,通常需要结合路由参数和动态路由匹配。以下是几种常见的实现方式: 使用查询参数实现分页 在路由…

Vue实现lagger页面

Vue实现lagger页面

Vue实现懒加载页面的方法 懒加载(Lazy Loading)是一种优化技术,用于延迟加载页面或组件,直到它们真正需要显示时才加载。以下是几种在Vue中实现懒加载页面的常见方法: 使用Vue Rou…

vue实现选项卡分页

vue实现选项卡分页

实现选项卡分页的基本思路 在Vue中实现选项卡分页功能,可以通过动态组件或条件渲染结合数据绑定完成。核心逻辑是维护一个当前激活的选项卡状态,根据用户点击切换内容。 使用v-if条件渲染 通过v-if…

js实现页面跳转

js实现页面跳转

使用 window.location.href 通过修改 window.location.href 属性实现页面跳转: window.location.href = 'https://exampl…

js实现分页功能代码

js实现分页功能代码

分页功能实现方法 客户端分页(前端处理数据) 适用于数据量较小且已全部加载到前端的情况。 // 示例数据 const data = Array.from({length: 100}, (_, i)…

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 Vue 中实现页面切换通常使用 Vue Router,这是 Vue.js 官方的路由管理器。以下是几种常见的实现方式: 使用 Vue Router 的基本配置 安装 Vu…