Preface: There is such a requirement in the development task that more than 5,000 pieces of data need to be displayed in the drop-down box, or even more. The amount of data is directly stuck on the entire page, so I thought of adding pagination in the drop-down box. There are two ways to achieve it:
Method 1: Use el-select and el-pagination in elementui to realize pagination
HTML部分:
<el-select
v-model="value1"
placeholder="请选择数据"
:clearable="false"
style="width: 240px"
size="mini"
filterable
:filter-method="filter"
>
<el-option
v-for="item in optionfen"
:key="item.value"
:label="item.value"
remote
:value="item.label"
placeholder="请输入"
>
</el-option>
<div style="bottom: -10px">
<el-pagination
small
@current-change="handleCurrentChange"
:current-page="currentpage"
:page-size="pageSize"
layout="prev, pager,next,total"
:total="options.length"
>
</el-pagination>
</div>
</el-select>
JS部分
export default {
data() {
return {
options: [], //总数据
optionfen: [], //当前页码的数据
value1: "", //输入框的值
currentpage: 1, //当前页码
pageSize: 10, //每页展示的条数
};
},
methods: {
//分页的实现,currentpage 为页码,每页展示的数据为10(可自行更改pageSize)条,分别是在总数据options中
//下标从(currentpage -1)*10开始的十条数据
handleCurrentChange(val) {
this.optionfen = [];
this.currentpage = val;
let start = (val - 1) * this.pageSize;
let end = Number(start) + Number(this.pageSize);
//此处需要判断如果计算的结束下标大于总数据的长度,则需要修改结束下标
if (end > this.options.length) {
end = this.options.length;
}
for (let i = start; i < end; i++) {
//将取到的数据展示在页面
this.optionfen.push(this.options[i]);
}
},
};
Method 2: Use the v-selectpage component to realize pagination search
v-selectpage official document address: https://terryz.gitee.io/vue/#/selectpage/demo
Steps:
1. Install v-selectpage dependencies
npm install v-selectpage@2.1.4 -save
2. Register to the Vue instance in main.js
import vSelectPage from 'v-selectpage'
Vue.use(vSelectPage, {
language: 'cn',
placeholder: '请选择数据'
})
3. Use in the index.vue page
<v-selectpage :data="options" v-model="value" show-field="name" key-field="value"></v-selectpage>
//其他的属性参考官方文档
Note: v-selectpage can not only realize the single selection of the drop-down box, but also realize the function points such as multi-line and form. For details, go to the official website to check