当我们向后台请求大量数据的时候,并要在页面展示出来,请求的数据可能上百条数据或者更多的时候,并不想在一个页面展示,这就需要使用分页功能来去完成了。

1.vue2.0+element-ui实现一个分页功能,element-ui这个组件特别丰富,分页中给我提供了一个Pagination 分页,使用Pagination 快速完成分页功能
<el-table :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)" style="width: 100%">
<el-table-column type="selection" width="55"></el-table-column>
<el-table-column prop="id" label="编号" show-overflow-tooltip width="auto"></el-table-column>
<el-table-column prop="goods_name" label="商品名称" show-overflow-tooltip width="auto"></el-table-column>
<el-table-column prop="goods_spec" label="商品规格" width="auto"></el-table-column>
<el-table-column prop="store_name" label="商家名称" width="auto"></el-table-column>
<el-table-column prop="order_sn" label="订单编号" width="auto"></el-table-column>
<el-table-column prop="stock" label="库存" width="auto">
<template slot-scope="scope">
<el-button type="text" size="mini" v-if="scope.row.stock>0">入库</el-button>
<el-button type="text" size="mini" style='color: #11b559;' v-if="scope.row.stock<0">出库</el-button>
</template>
</el-table-column>
<el-table-column prop="ctime" label="日志时间" width="auto"></el-table-column>
</el-table>
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[5, 10, 20, 40]" //这是下拉框可以选择的,每选择一行,要展示多少内容
:page-size="pagesize" //显示当前行的条数
layout="total, sizes, prev, pager, next, jumper"
:total="tableData.length"> //这是显示总共有多少数据,
</el-pagination>
需要data定义一些,tableData定义一个空数组,请求的数据都是存放这里面
data () {
return {
currentPage:1, //初始页
pagesize:10, // 每页的数据
tableData: []
}
},
methods: {
// 初始页currentPage、初始每页数据数pagesize和数据data
handleSizeChange: function (size) {
this.pagesize = size;
console.log(this.pagesize) //每页下拉显示数据
},
handleCurrentChange: function(currentPage){
this.currentPage = currentPage;
console.log(this.currentPage) //点击第几页
},
handleUserList() {
this.$http.get('http://localhost:3000/userList').then(res => { //这是从本地请求的数据接口,
this.tableData= res.body
})
}
}
以上都是分页所需的功能,也是自己在自己写案例中所遇到的,并总结下方便查看嘿嘿
网友评论