新建指令 :
1、 在 src 目录下 新建文件夹 directive / loadmore
2、在 loadmore 文价夹下新建 elSelectLoadmore.js 和 index.js 文件:
elSelectLoadmore.js
export default {
inserted(el, binding, vnode) {
const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
SELECTWRAP_DOM.addEventListener('scroll', function() {
const condition = Math.floor(this.scrollHeight - this.scrollTop) <= this.clientHeight;
if (condition) {
binding.value();
}
});
}
};
index.js
import elSelectLoadmore from './elSelectLoadmore';
const install = function(Vue) {
Vue.directive('el-select-loadmore', elSelectLoadmore);
};
if (window.Vue) {
window['el-select-loadmore'] = elSelectLoadmore
Vue.use(install); // eslint-disable-line
}
export default install;
3、在main 文件中注册该指令
import elSelectLoadmore from './directive/loadmore'
Vue.use(elSelectLoadmore)
4、 基于 el-select 封装 懒加载 远程搜索框
remoteSelect.vue
<template>
<el-select
ref="remoteSelect"
:disabled="disabled"
style="width:100%"
v-model="remoteCode"
filterable
remote
clearable
reserve-keyword
placeholder="请输入软件id / 软件名称"
:remote-method="remoteMethod"
v-el-select-loadmore="loadmore"
:loading="loading"
@input="input"
:multiple="multiple"
>
<el-option
v-for="item in options"
:key="item.id"
:label="item.id + ' ' +item.name"
:value="item.id"
>
</el-option>
<div class="uploadTip"
v-if="loadMoreLoading && !loadNoMore"
>数据加载中......</div>
<div
class="uploadTip"
v-if="loadNoMore"
>暂无更多数据</div>
</el-select>
</template>
<script>
import { getSoftwareForRef } from "@/api/softwar/manage";
export default {
name:'remote-select',
props:{
value:{
type:[Number, String, Array],
default:undefined
},
disabled:{
type: Boolean,
default:false
},
multiple:{
type:Boolean,
default:false
},
status:{
type:Number,
default:undefined
}
},
data(){
return {
remoteQuery:{
pageNum:1,
pageSize:10,
status:this.status //接口状态参数
},
options:[],
loading:false, // 组件搜索加载中参数
loadMoreLoading:false, // 翻页加载中参数
remoteCode:undefined, // select value
loadNoMore:false // 没有更多的状态
}
},
mounted(){
},
watch:{
value: {
handler(data) {
this.remoteCode = data
},
immediate: true
},
},
methods: {
input(val){
this.$emit('input',val)
const index = this.options.findIndex(i => i.id === val)
this.$emit('inputAll', index === -1 ? val : this.options[index])
},
remoteMethod(query, review) {
this.loading = true
this.loadNoMore = false
this.remoteQuery.pageNum = 1
if(typeof query === 'object'){
this.remoteQuery.swIds = query
}else{
this.remoteQuery.param = query
delete this.remoteQuery.swIds
}
this.remoteQuery.status = this.status
getSoftwareForRef(this.remoteQuery).then(res => {
this.options = res.data
this.loading = false;
if(typeof review === 'boolean' && review){
let index = this.options.findIndex(i => i.id === this.value)
if(index !== -1){
this.remoteCode = this.value + ' ' + this.options[index].name
}else{
this.remoteCode = this.value
}
}
})
},
loadmore(){
if(this.loadMoreLoading){
return
}
var remoteQuery = {...this.remoteQuery};
remoteQuery.pageNum++;
this.loadMoreLoading = true
getSoftwareForRef(remoteQuery).then(res => {
if(res.code === 0 && typeof res.data === 'object' && res.data.length){
this.options = this.options.concat(res.data)
this.remoteQuery.pageNum++
}
if(res.code === 0 && typeof res.data === 'object' && res.data.length === 0){
this.loadNoMore = true;
}
this.$nextTick(() => {
setTimeout(() => {
this.loadMoreLoading = false
}, 500)
})
}).catch(err => {
this.$nextTick(() => {
setTimeout(() => {
this.loadMoreLoading = false
}, 500)
})
})
},
setValue(){
typeof this.value === 'object' ? this.remoteMethod(this.value) : this.remoteMethod(this.value, true)
},
clearOptions(){
this.options = []
}
},
}
</script>
<style scoped>
.uploadTip{
width: 100%;
text-align: center;
}
</style>
组件提示 tips:
由于组件搜索下拉框分页通过 scroll 事件实现的
所以远程加载提示涉及到元素隐藏和显示会一直触发 scroll 到底事件;
需要常驻!!!!文章来源:https://www.toymoban.com/news/detail-809951.html
错误例子: 在 options 底部加载中显示 <div>加载中。。。</div> 接口请求结束在隐藏文章来源地址https://www.toymoban.com/news/detail-809951.html
到了这里,关于Vue2 + element ui el-select 远程搜索分页懒加载功能实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!