element下拉框,滚动加载更多数据

element下拉框没有自带下拉加载更多数据的操作,所以需要自定义一些指令

第一步:新建一个JS

import Vue from vue

Vue.directive(loadmore, {
  bind (el, binding) {
    // 获取element-ui定义好的scroll盒子
    const SELECTWRAP_DOM = el.querySelector(.el-select-dropdown .el-select-dropdown__wrap)
    SELECTWRAP_DOM.addEventListener(scroll, function () {
      const CONDITION = this.scrollHeight - this.scrollTop === this.clientHeight;
      if (CONDITION == false) {
        binding.value()
      }
    })
  }
})


第二部:在main.js引用新建的js
// 注册滚动条加载触发事件v-loadmore绑定
import selectDirectives from @/utils/selectDrectives
Vue.use(selectDirectives)

第三步:在组件中使用
   <el-select data-type="normal" filterable v-model="form.lineNo" **v-loadmore="loadMore"**>
                <el-option v-for="(item,index) in lineOptions" :key="index" :label="item.lineName" :value="item.lineNo"></el-option>
              </el-select>
         
         注意:这里的loadMore需要在methods中自定义请求操作方法
         
         methods:{
           loadMore(){
  
        this.getList();  //这我是请求下拉框数据的方法
      },           
   //获取下拉框数据
      getList(){
      //这里的canmore,需要在data中定义,默认为true
        if(!**this.canmore**){
          return
        }
        this.data.pageNum++;
        getLineTableList(this.data).then(res=>{
          // this.lineOptions = res.rows;
          if(res.rows.length>0){
            this.lineOptions = this.lineOptions.concat(res.rows);
          }else{
            this.canmore=false;
          }
        })

      },
}
经验分享 程序员 微信小程序 职场和发展