vue elementui的select组件实现滑到底部分页请求后端接口

vue elementui的select组件实现滑到底部分页请求后端接口

1.实现效果

老规矩,直接上最后的实现效果

2.实现原理

直接上代码

javascript 复制代码
   <el-form-item class="diagmosisItem" label="诊断" v-scroll="handleScroll">
            <el-select
            size="small"
              remote
              filterable
              clearable
              :loading="getAllDiagnosisLoading"
              v-model="queryObj.diagnosisDesc"
              :remote-method="handleRemoteDisease"
              @clear="handleClearDisease"
              >
              <el-option
                v-for="item in allDiagnosisList"
                :key="item.valueId"
                :label="item.valueNo +' '+ item.valueDesc"
                :value="item.valueDesc">
              </el-option>

            </el-select>
          </el-form-item>
//js
//mothods
    handleScroll() {
      if(!this.scrollStop) {
        this.diagnosisQuery.pageNo++
        this.getAllDiagnosis(this.diagnosisQueryText, 'join')
      }
    },
    // 远程搜索诊断
    async handleRemoteDisease(keyword = '') {
      this.diagnosisQueryText = keyword
      this.getAllDiagnosis(keyword)
    },
    // 清除选中诊断
    handleClearDisease() {
      this.getAllDiagnosis('', 'clear')
    },
     //诊断列表
    async getAllDiagnosis(val = '', type = 'search') {
      try {
        this.getAllDiagnosisLoading = true
        this.scrollStop = false
        let res = null
        if(this.isHaveDiagnoseFlag) {
          if(type =='search') {
            this.diagnosisQuery ={
              pageNo:0,
              pageSize:100
            }
            res = await this.reqGetAllDiagnosis({
              keyword:val,
              pageNo:0,
              pageSize:100
            })
          }
          else if(type == 'join') {
            res = await this.reqGetAllDiagnosis({
              keyword:val,
              ...this.diagnosisQuery
            })
          }
          else{
            this.allDiagnosisList = this.allDiagnosisList
            this.getAllDiagnosisLoading = false
          }

        }
        if (res && res.success) {
          if(type =='search') {
            this.allDiagnosisList = res.data
          }
          else{
            if(res.data.length == 0) {
              this.scrollStop = true
            }
            this.allDiagnosisList = [...res.data, ...this.allDiagnosisList]
          }
          this.getAllDiagnosisLoading = false
        }
      } catch (error) {
        this.getAllDiagnosisLoading = false
      }
    },
//主要看这里
  directives:{
    scroll:{
      bind(el, binding) {
        const SELECTNRAP_DON = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
        SELECTNRAP_DON.addEventListener( 'scroll', function() {
          console.log(this.scrollHeight - this.scrollTop, this.clientHeight)
          const CONDITION = this.scrollHeight - this.scrollTop <= this.clientHeight
          if(CONDITION) {
            binding.value()
          }
        })
      }
    }
  },

scrollStop主要是用来诊断select移到底部不再请求数据,默认为false。思路反正就是到底了触发函数处理,pageNo++请求后端接口

相关推荐
Uyker27 分钟前
从零开始制作小程序简单概述
前端·微信小程序·小程序
EndingCoder5 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客6 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro6 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom7 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...7 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡8 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜059 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx9 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9999 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序