vue+elementUI组件table实现前端分页功能

前端分页和后端分页就是请求的差异,前端分页的话只请求一次,所以要在render table组件的时候控制下数据

:data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"   //不多解释

再就是设置分页器total等于table数据的长度

:total="tableData.length"

点击分页器的操作

methods: { handleSizeChange(val) { console.log(`每页 ${val} 条`); this.currentPage = 1; this.pageSize = val; }, handleCurrentChange(val) { console.log(`当前页: ${val}`); this.currentPage = val; } }

千言万语不如上个demo~

<template> <div> <el-table :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)"> <el-table-column prop="date" label="日期"> </el-table-column> <el-table-column prop="name" label="姓名"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> <!-- 分页器 --> <div> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[1,5,10,20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="tableData.length"> </el-pagination> </div> </div> </template> <script> export default { data() { return { tableData: [ { date: "2016-05-02", name: "王小虎", address: "上海市普陀区金沙江路 1518 弄" }, { date: "2016-05-04", name: "王小虎", address: "上海市普陀区金沙江路 1517 弄" }, { date: "2016-05-01", name: "王小虎", address: "上海市普陀区金沙江路 1519 弄" }, { date: "2016-05-03", name: "王小虎", address: "上海市普陀区金沙江路 1516 弄" } ], currentPage: 1, // 当前页码 total: 20, // 总条数 pageSize: 1 // 每页的数据条数 }; }, methods: { handleSizeChange(val) { console.log(`每页 ${val} 条`); this.currentPage = 1; this.pageSize = val; }, handleCurrentChange(val) { console.log(`当前页: ${val}`); this.currentPage = val; } } }; </script>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/45a9c04aa593664555046178a6d561c6.html