element-ui的分页只有固定的页数选择,我们可以对其进行封装,变成自定义输入进行分页.
效果如下:
代码如下:
<template>
<div class="sui-pagination">
<div class="page-input">
<span>每页显示</span>
<el-input
class="small"
style="padding:0 5px;"
type="number"
min="0"
v-model.number="inputNum"
@keyup.enter.native="setPage"
></el-input>
<span>条,输入后按回车键</span>
</div>
<el-pagination
:background="true"
:current-page="params.pageNo"
layout="total, prev, pager, next, jumper"
:page-size="params.pageSize"
:total="total"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
style="text-align:right;"
></el-pagination>
</div>
</template>
<script>
export default {
name:"sui-pagination",
props:{
total:{
type:Number,
default:0
},
params: {
type:Object,
default:{
pageNo:1,
pageSize:10
}
}
},
data() {
return {
inputNum: 10, // 默认输入的条数
}
},
methods: {
setPage(event) {
let num = event.target.value
let re = /^[0-9]+$/ // 做一下数据过滤
let state = re.test(num)
if (state) {
this.handleSizeChange(num);
} else {
this.handleSizeChange(this.inputNum);
}
},
handleCurrentChange(val) {
this.$emit('change', {
pageNo:val,
pageSize:this.params.pageSize
})
},
handleSizeChange(val) {
this.$emit('change', {
pageNo:this.params.pageNo,
pageSize:val
})
}
},
}
</script>
<style lang="scss" scoped>
.sui-pagination {
display: flex;
}
.page-input {
display: inline-block;
display: flex;
align-items: center;
margin:15px 35px 0px 0px;
> span {
display: inline-block;
color: #8a8e98;
font-size: 14px;
user-select: none;
white-space: nowrap;
}
.small {
width: 78px;
padding: 0 5px;
}
}
</style>
使用方法:
<sui-pagination
class="width-47"
:total="total"
:params="params"
@change="pageChanges">
</sui-pagination>
params:{
pageNo:,
pageSize:,
...
}
total:总数
pageChanges(params) {
this.params.pageNo = params.pageNo;
this.params.pageSize = params.pageSize;
this.fetchData(); // 执行接口分页查询
},