当前位置: 首页 > news >正文

vue3 element-plus el-option滚动分页

表单使用场景**HTML**<el-form-item label="名称"prop="merchantId"ref="merchantIdRef"><ScrollSelect v-model="form.merchantId":options="merchantOptions":loading="merchantLoading":has-more="hasMoreMerchant"popper-class="merchantIdPopper"placeholder="请选择名称"class="filter-option filter-h36"clearable remote filterable @load-more="handleMerchantLoadMore"@remote-method="handleMerchantRemoteMethod"@focus="handleMerchantFocus"/></el-form-item>**JS代码**// 商家列表数据constmerchantOptions=ref([])constmerchantQueryParams=reactive({current:1,size:10,keyword:""})constmerchantLoading=ref(false)consthasMoreMerchant=ref(true)constgetMerchantOptions=(isLoadMore=false)=>{if(merchantLoading.value||(!hasMoreMerchant.value&&isLoadMore))returnmerchantLoading.value=truemerchantApi.merchantPage(merchantQueryParams).then(response=>{if(response.code===200){constnewOptions=response.data.records.map(item=>({label:item.name,value:item.id,}))merchantOptions.value=isLoadMore?[...merchantOptions.value,...newOptions]:newOptions hasMoreMerchant.value=merchantOptions.value.length<response.data.total}}).finally(()=>{merchantLoading.value=false})}// 滚动加载更多consthandleMerchantLoadMore=()=>{merchantQueryParams.current++getMerchantOptions(true)}// 获取焦点时加载初始数据consthandleMerchantFocus=()=>{if(merchantOptions.value.length===0){merchantQueryParams.current=1merchantQueryParams.keyword=''hasMoreMerchant.value=truegetMerchantOptions()}}// 远程搜索consthandleMerchantRemoteMethod=(query)=>{merchantQueryParams.current=1merchantQueryParams.keyword=query hasMoreMerchant.value=truegetMerchantOptions()}```-```javascript 表单使用场景**HTML**<el-form-item label="名称"prop="merchantId"ref="merchantIdRef"><ScrollSelect v-model="form.merchantId":options="merchantOptions":loading="merchantLoading":has-more="hasMoreMerchant"popper-class="merchantIdPopper"placeholder="请选择名称"class="filter-option filter-h36"clearable remote filterable @load-more="handleMerchantLoadMore"@remote-method="handleMerchantRemoteMethod"@focus="handleMerchantFocus"/></el-form-item>**JS代码**// 商家列表数据constmerchantOptions=ref([])constmerchantQueryParams=reactive({current:1,size:10,keyword:""})constmerchantLoading=ref(false)consthasMoreMerchant=ref(true)constgetMerchantOptions=(isLoadMore=false)=>{if(merchantLoading.value||(!hasMoreMerchant.value&&isLoadMore))returnmerchantLoading.value=truemerchantApi.merchantPage(merchantQueryParams).then(response=>{if(response.code===200){constnewOptions=response.data.records.map(item=>({label:item.name,value:item.id,}))merchantOptions.value=isLoadMore?[...merchantOptions.value,...newOptions]:newOptions hasMoreMerchant.value=merchantOptions.value.length<response.data.total}}).finally(()=>{merchantLoading.value=false})}// 滚动加载更多consthandleMerchantLoadMore=()=>{merchantQueryParams.current++getMerchantOptions(true)}// 获取焦点时加载初始数据consthandleMerchantFocus=()=>{if(merchantOptions.value.length===0){merchantQueryParams.current=1merchantQueryParams.keyword=''hasMoreMerchant.value=truegetMerchantOptions()}}// 远程搜索consthandleMerchantRemoteMethod=(query)=>{merchantQueryParams.current=1merchantQueryParams.keyword=query hasMoreMerchant.value=truegetMerchantOptions()}```支持 v-model 双向绑定 - 支持远程查询(remote-method) - 支持滚动加载更多(load-more) - 支持焦点事件(focus) - 支持可见性变化事件(visible-change) **下拉选择通用组件**```javascript<template><el-select v-bind="$attrs"v-model="internalValue":popper-class="popperClass":remote-method="handleRemoteMethod"@visible-change="handleVisibleChange"@focus="handleFocus"><el-option v-for="option in options":key="option.value":label="option.label":value="option.value"/><div v-if="loading"class="loading-more"><el-iconclass="is-loading"><Loading/></el-icon><span>加载中...</span></div><div v-else-if="!hasMore"class="no-more">没有更多数据</div></el-select></template><script setup>import{ref,watch,onMounted}from'vue'import{Loading}from'@element-plus/icons-vue'constprops=defineProps({modelValue:{type:[String,Number,Boolean],default:''},options:{type:Array,default:()=>[]},loading:{type:Boolean,default:false},hasMore:{type:Boolean,default:true},popperClass:{type:String,default:'scroll-select-popper'}})constemit=defineEmits(['update:modelValue','visible-change','load-more','remote-method','focus'])consthandleFocus=()=>{emit('focus')}consthandleRemoteMethod=(query)=>{emit('remote-method',query)}constinternalValue=ref(props.modelValue)// 监听 modelValue 的变化,更新内部值watch(()=>props.modelValue,(newValue)=>{internalValue.value=newValue})// 监听内部值的变化,更新父组件的值watch(internalValue,(newValue)=>{emit('update:modelValue',newValue)})consthandleVisibleChange=(visible)=>{emit('visible-change',visible)if(visible){setTimeout(()=>{setupScrollListener()},100)}else{removeScrollListener()}}constsetupScrollListener=()=>{constpopper=document.querySelector(`.${props.popperClass}.el-select-dropdown__wrap`)if(popper){popper.removeEventListener('scroll',handleScroll)popper.addEventListener('scroll',handleScroll)}}constremoveScrollListener=()=>{constpopper=document.querySelector(`.${props.popperClass}.el-select-dropdown__wrap`)if(popper){popper.scrollTop=0popper.removeEventListener('scroll',handleScroll)}}consthandleScroll=(e)=>{const{scrollTop,scrollHeight,clientHeight}=e.targetif(scrollTop+clientHeight>=scrollHeight-10&&!props.loading&&props.hasMore){emit('load-more')}}</script><style scoped>.loading-more,.no-more{text-align:center;padding:10px0;font-size:14px;color:#909399;}.loading-more.is-loading{margin-right:5px;}</style>

调用组件

在表单使用情景**HTML**<el-form-item label="名称"prop="merchantId"ref="merchantIdRef"><ScrollSelect v-model="form.merchantId":options="merchantOptions":loading="merchantLoading":has-more="hasMoreMerchant"popper-class="merchantIdPopper"placeholder="请选择名称"class="filter-option filter-h36"clearable remote filterable @load-more="handleMerchantLoadMore"@remote-method="handleMerchantRemoteMethod"@focus="handleMerchantFocus"/></el-form-item>**JS代码**// 商家列表constmerchantOptions=ref([])constmerchantQueryParams=reactive({current:1,size:10,keyword:"",})constmerchantLoading=ref(false)consthasMoreMerchant=ref(true)constmerchantSelectVisible=ref(false)constgetMerchantOptions=(isLoadMore=false)=>{if(merchantLoading.value||(!hasMoreMerchant.value&&isLoadMore))returnmerchantLoading.value=truemerchantApi.merchantPage(merchantQueryParams).then(response=>{if(response.code===200){constnewOptions=response.data.records.map(item=>({label:item.name,value:item.id,}))if(isLoadMore){merchantOptions.value=[...merchantOptions.value,...newOptions]}else{merchantOptions.value=newOptions}// 检查是否还有更多数据hasMoreMerchant.value=merchantOptions.value.length<response.data.total}else{// 接口请求失败时的处理// proxy.$modal.msgError('获取商家列表失败')}}).catch(()=>{// 网络错误时的处理// proxy.$modal.msgError('网络错误,请稍后重试')}).finally(()=>{merchantLoading.value=false})}// 滚动查询consthandleMerchantLoadMore=()=>{merchantQueryParams.current++getMerchantOptions(true)}consthandleMerchantFocus=()=>{// 当获取焦点时,如果还没有数据,加载初始数据if(merchantOptions.value.length===0){merchantQueryParams.current=1merchantQueryParams.keyword=''hasMoreMerchant.value=truegetMerchantOptions()}}// 远程关键字查询consthandleMerchantRemoteMethod=(query)=>{// 重置分页参数merchantQueryParams.current=1merchantQueryParams.keyword=query hasMoreMerchant.value=true// 重新加载数据getMerchantOptions()}
http://www.jsqmd.com/news/695606/

相关文章:

  • 计算机毕业设计:Python股市交易后台管理系统 Django框架 requests爬虫 数据分析 可视化 大数据 大模型(建议收藏)✅
  • 深入TI DSP的EPWM影子寄存器:为什么以及如何正确使用它?
  • 空调行业“铜铝之争”深度解析:从技术探讨到舆论大战,理性回归正当时
  • Kylin麒麟操作系统查询防火墙状态及端口开放
  • 在Ubuntu 22.04上从源码编译安装gnina 1.1:一个生物信息学新手的踩坑与填坑全记录
  • FastDFS 分布式存储
  • 如何轻松实现i茅台自动预约:告别早起抢购的终极解决方案
  • 彩云岛去水印
  • 暗黑破坏神2角色编辑器:5分钟掌握Diablo Edit2终极指南
  • 光伏MMC并网系统(两级式)交流故障穿越与电网对称与不对称故障:simulink仿真模型及光伏经模
  • 别再只读ADC值了!STM32标准库下光敏传感器的校准与标定实战
  • Python脚本参数传递与命令行工具开发实战
  • 别再手动加标签了!用MATLAB的text函数给你的图表自动添加专业注释(附TeX公式教程)
  • 无人机视角田间土豆马铃薯苗和杂草检测数据集VOC+YOLO格式384张5类别
  • MySQL主从复制支持跨版本吗_不同版本间同步的注意事项
  • 电话营销机器人,智能语音外呼获客系统
  • 从厨房秤到智能仓储:HX711的增益、标定与线性拟合,让你的项目精度提升一个档次
  • 盘古50K开发板PCIE性能初探:如何利用PGL50H的HSST高速收发器进行通信验证
  • SVD降维技术解析与Python实战指南
  • OceanBase-Desktop-Setup-1.0.0.exe
  • OpenUI:用流式语言标准解决AI生成UI的解析与渲染难题
  • 框架之战——Infoseek舆情系统解析回应如何塑造公众认知
  • 5分钟免费升级:如何在Word中启用APA第7版参考文献格式
  • 完整指南:如何在UKB_RAP上高效完成生物医学数据分析的5个关键步骤
  • 2026年自贡补牙根管机构排行:自贡整牙,自贡替牙期牙齿矫正,自贡根尖周炎治疗,自贡正畸,优选指南! - 优质品牌商家
  • 机器学习中的距离度量:原理、实现与应用
  • 炸场!不排队的满血Seedance 2.0原生1080P登陆AniShort,AI短剧画质天花板来了
  • OceanBase-Desktop-Setup-1.6.0.exe
  • 预警响应闭环与历史数据能力——Infoseek舆情系统谈两个被忽视的基础设施
  • 告别图片格式烦恼:Chrome右键菜单的格式转换神器