当前位置:首页 > 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
}

在组件中使用:

vue实现页面分页

<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>

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

相关文章

h5实现登录页面跳转页面跳转页面跳转

h5实现登录页面跳转页面跳转页面跳转

使用HTML5实现登录页面跳转 在HTML5中实现登录页面跳转可以通过多种方式完成,包括表单提交、JavaScript跳转和超链接跳转。以下是几种常见的实现方法。 表单提交跳转 使用HTML表单的a…

vue实现页面定位

vue实现页面定位

Vue 实现页面定位的方法 使用 scrollIntoView 方法 通过 JavaScript 的 scrollIntoView 方法可以实现平滑滚动到指定元素位置。在 Vue 中,可以通过 ref…

vue实现页面切换

vue实现页面切换

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

jquery页面跳转

jquery页面跳转

jQuery 页面跳转方法 使用 jQuery 实现页面跳转可以通过多种方式完成,以下是几种常见的方法: 使用 window.location.href 通过修改 window.location.h…

vue页面实现流程

vue页面实现流程

Vue 页面实现流程 创建 Vue 项目 使用 Vue CLI 或 Vite 初始化项目。Vue CLI 适用于传统项目,Vite 适合现代轻量级应用。 安装依赖后,通过命令行工具运行项目初始化命令,…

vue 实现页面跳转

vue 实现页面跳转

Vue 实现页面跳转的方法 在 Vue 中实现页面跳转可以通过多种方式,主要包括使用 Vue Router 提供的导航方法和原生 JavaScript 的方式。 使用 Vue Router 的 ro…