给element中下拉选项框el-select提供拉到底加载下一页数据
1、新增一个自定义指令
directives: {
'el-select-loadmore': {
bind(el, binding) {
// 获取element-ui定义好的scroll盒子
const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
SELECTWRAP_DOM.addEventListener('scroll', function () {
/**
* scrollHeight 获取元素内容高度(只读)
* scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚 动 条, 那它的scrollTop的值默认为0.
* clientHeight 读取元素的可见高度(只读)
* 如果元素滚动到底, 下面等式返回true, 没有则返回false:
* ele.scrollHeight - ele.scrollTop === ele.clientHeight;
*/
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
if (condition) {
binding.value();
}
});
}
}
},
2、在下拉选择框新增自定义指令
v-el-select-loadmore=“loadMore”
如:
<el-select :disabled="operate == 'EDIT'" v-el-select-loadmore="loadMore" clearable filterable placeholder="请选择渠道" remote style="width:100%" v-model="model.id">
<el-option :key="item.id" :label="item.id" :value="item.id"
v-for="item in options">
<span style="float: left">{{ item.id}}</span>
<span style="float: right; color: #8492a6; font-size: 13px">{{ item.name}}</span>
</el-option>
</el-select>
3、data中新增需要用到的参数
options: [],
formData: {
pageIndex: 0,
pageSize: 10,
},
acLoadMore: true,
4、新增函数用于添加数据
// 查询渠道信息
loadMore() {
this.formData.pageIndex = this.formData.pageIndex + 1;
if (this.acLoadMore) {
this.getList(this.formData);
}
},
getList(formData) {
let action = {
model: {},
offset: Number((this.formData.pageIndex) * this.formData.pageSize),
limit: this.formData.pageSize,
};
kayak.rpc('admin.channel.ListChannel', action).then(result => {
if (result.list.data.length == 0) {
this.acLoadMore = false;
}
const _res = result.list.data; // 请求得到的数据
this.options = [...this.options, ..._res];
});
},
5、记住在进入页面create创建时调用获取数据的函数
this.getList();//获取下拉列表第一页内容