element-ui select 下拉框做成下拉加载更多

注意: vue 版本需要 ≥ 3.3

1、html

<el-select
	v-model="relation_type"
	placeholder="请选择合作类型"
	ref="select"
>
	<el-option
		v-for="item in cooperationTypeList"
		:key="item.value"
		:label="item.label"
		:value="item.value"
	/>
</el-select>

2、主文件

import { useElSelectionInfinityScroll } from '@/utils/combinationFunc';
setup(props, context) {
	const data = reactive({
		noMore: false,
		loading: false,
		cooperationTypeList: []
	});
	const { proxy } = getCurrentInstance();
	const loadMore = () => {
		if (data.loading) return;
		data.loading = true;
		if (proxy.cooperationTypeList.length > 40) {
			// 获取到最后的值时,不再监听滚动条的动作,移除滚动事件
			data.noMore = true;
		}
		proxy.cooperationTypeList.push(...proxy.cooperationTypeList);
		data.loading = false;
	};
	onMounted(() => {
		const elem = proxy.$refs.select.$refs.scrollbar.$refs.wrap;
		useElSelectionInfinityScroll(elem, loadMore, () => data.noMore);
	});
}

自行补充接口调用相关方法

3、 @/utils/combinationFunc.js

import { onUnmounted, toValue, watchEffect } from 'vue';
import { Throttle } from '@/utils/debunce';

export function useElSelectionInfinityScroll(target, callback, noMore) {
	onUnmounted(() => target.removeEventListener('scroll', Throttle(scolling, 300)));
	const scolling = () => {
		if (toValue(noMore)) return;
		const canLoadMore = target.scrollHeight - target.scrollTop <= target.clientHeight;
		if (canLoadMore) {
			callback();
		}
	};
	watchEffect(() => { scolling(); });
	target.addEventListener('scroll', scolling);
}

最近更新

  1. TCP协议是安全的吗?

    2024-05-09 07:04:10       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-09 07:04:10       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-09 07:04:10       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-09 07:04:10       20 阅读

热门阅读

  1. ubuntu arm64 阿里云源

    2024-05-09 07:04:10       13 阅读
  2. Ubuntu 22.04 下 Pycharm 卸载

    2024-05-09 07:04:10       9 阅读
  3. STM32中的Systick的使用

    2024-05-09 07:04:10       12 阅读
  4. 初识Node.js-回调函数(详解回调函数使用)

    2024-05-09 07:04:10       11 阅读
  5. 从PostgreSQL同步数据到Elasticsearch

    2024-05-09 07:04:10       10 阅读
  6. 智能BI(后端) -- 智能分析业务

    2024-05-09 07:04:10       11 阅读
  7. Python 基础知识:入门指南

    2024-05-09 07:04:10       10 阅读
  8. three.js 中ShaderChunk的 uv_pars_vertex.glsl

    2024-05-09 07:04:10       10 阅读