vue3+element 滚动触底加载选择器

html 复制代码
<template>
  <el-select v-model="selectedValue" filterable :placeholder="placeholder" @visible-change="handleVisibleChange"
    @focus="handleFocus" popper-class="cit-scroll-select">
    <el-option v-for="item in props.options" :key="item[dataConfig.value]" :label="item[dataConfig.name]"
      :value="item[dataConfig.value]" />
    <template #footer v-if="showFooter">
      <div class="text-center">
        <span v-if="props.loading && props.options?.length < props.total">
          {{ props.loading ? '加载中...' : '' }}
        </span>
        <span v-else-if="props.options?.length === props.total">
          没有更多数据了
        </span>
      </div>
    </template>
  </el-select>
</template>

<script setup>
/**
* @author        全易
* @time          2025-12-08 17:38:17 星期一
* @description   触底加载 选择器
* @example       <scroll-select v-model="value" :options="options" :loading="loading" :total="total" @initData="getData" @reachBottom="reachBottom" />
**/

const selectedValue = defineModel()
const props = defineProps({
  loading: {
    type: Boolean,
    default: false
  },
  options: {
    type: Array,
    default: () => []
  },
  total: {
    type: Number,
    required: true
  },
  placeholder: {
    type: String,
    default: ''
  },
  queryForm: {
    type: Object,
    default: () => ({})
  },
  // 取值配置
  dataConfig: {
    type: Object,
    default: () => ({
      name: 'name',
      value: 'id'
    })
  }
})

// 显示底部加载更多
const showFooter = computed(() => {
  if (props.queryForm?.pageNum === 1) { return false }
  return (props.loading && props.options?.length < props.total) || (props.options?.length === props.total)
})

const showDefaultOption = computed(() => {
  // 当选中值不在options中时显示默认选项
  if (!selectedValue.value) return false;
  return !props.options.some(option => option.value === selectedValue.value);
})

const emits = defineEmits(['reachBottom'])
// 选择器显示/隐藏处理
const handleVisibleChange = (visible) => {
  if (visible && props.options?.length === 0) {
    emits('initData')
  }
}

// 获取焦点时加载数据
const handleFocus = () => {
  if (props.options?.length === 0) {
    emits('reachBottom')
  }
}

// 监听下拉框滚动事件
const setupScrollListener = () => {
  const popperEl = document.querySelector('.cit-scroll-select .el-select-dropdown__wrap')
  if (popperEl) {
    popperEl.addEventListener('scroll', handleScroll)
  }
}

// 移除滚动监听
const removeScrollListener = () => {
  const popperEl = document.querySelector('.cit-scroll-select .el-select-dropdown__wrap')
  if (popperEl) {
    popperEl.removeEventListener('scroll', handleScroll)
  }
}

// 滚动处理函数
const handleScroll = (e) => {
  const { scrollTop, scrollHeight, clientHeight } = e.target
  // 距离底部 60px 时开始加载
  if (scrollHeight - scrollTop - clientHeight < 60 && !props.loading && (props.options.length < props.total)) {
    emits('reachBottom')
  }
}

// 使用 MutationObserver 监听下拉框的出现
let observer = null
const initObserver = () => {
  observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      if (mutation.addedNodes.length) {
        const selectDropdown = document.querySelector('.cit-scroll-select .el-select-dropdown__wrap')
        if (selectDropdown) {
          setupScrollListener()
        }
      }
    })
  })

  // 监听 body 的变化
  observer.observe(document.body, {
    childList: true,
    subtree: true
  })
}

// 组件挂载时初始化
onMounted(() => {
  initObserver()
  // 初始加载数据
  emits('reachBottom')
})

// 组件卸载前清理
onBeforeUnmount(() => {
  if (observer) {
    observer.disconnect()
  }
  removeScrollListener()
})
</script>

<style scoped lang="scss"></style>
相关推荐
计算机学姐5 分钟前
基于SpringBoot的校园社团管理系统
java·vue.js·spring boot·后端·spring·信息可视化·推荐算法
沐墨染10 分钟前
Vue实战:自动化研判报告组件的设计与实现
前端·javascript·信息可视化·数据分析·自动化·vue
奔跑的呱呱牛12 分钟前
viewer-utils 图片预览工具库
javascript·vue·react
摘星编程23 分钟前
React Native鸿蒙:Image本地图片显示
javascript·react native·react.js
爱上妖精的尾巴1 小时前
8-5 WPS JS宏 match、search、replace、split支持正则表达式的字符串函数
开发语言·前端·javascript·wps·jsa
小温冲冲1 小时前
通俗且全面精讲单例设计模式
开发语言·javascript·设计模式
意法半导体STM322 小时前
【官方原创】FDCAN数据段波特率增加后发送失败的问题分析 LAT1617
javascript·网络·stm32·单片机·嵌入式硬件·安全
Liu.7742 小时前
vue开发h5项目
vue.js
咔咔一顿操作2 小时前
轻量无依赖!autoviwe 页面自适应组件实战:从安装到源码深度解析
javascript·arcgis·npm·css3·html5
刘联其2 小时前
.net也可以用Electron开发跨平台的桌面程序了
前端·javascript·electron