当前位置:网站首页>el-select如何实现懒加载(带搜索功能)
el-select如何实现懒加载(带搜索功能)
2022-07-04 05:29:00 【_处女座程序员的日常】
<!--
<template>
<el-select
:value="value"
v-loadmore="loadMore"
@focus="focus"
@clear="clear"
filterable
remote
:filter-method="handleSearch"
:loading="loading"
clearable
v-bind="$attrs"
v-on="$listeners"
>
<el-option
v-for="option in data"
:label="option[dictLabel]"
:value="option[dictValue]"
:key="option.value"
></el-option>
<!-- 此处加载中的value可以随便设置,只要不与其他数据重复即可 -->
<el-option
v-if="hasMore"
disabled
label="加载中..."
value="-1"
></el-option>
</el-select>
</template>
<script>
export default {
props: {
value: {
default: ""
},
// 列表数据
data: {
type: Array,
default: () => []
},
dictLabel: {
type: String,
default: "label"
},
dictValue: {
type: String,
default: "value"
},
// 调用页数的接口
request: {
type: Function,
default: () => {
}
},
// 传入的页码
page: {
type: [Number, String],
default: 1
},
// 是否还有更多数据
hasMore: {
type: Boolean,
default: true
}
},
directives: {
// 这里实现一个组件内部的自定义指令
loadmore: {
// 指令的定义
bind(el, binding) {
const SELECTWRAP = el.querySelector(
".el-select-dropdown .el-select-dropdown__wrap"
);
if (!SELECTWRAP) {
throw new Error('获取不到"el-select-dropdown__wrap"节点');
}
SELECTWRAP.addEventListener("scroll", () => {
// scrollTop 这里可能因为浏览器缩放存在小数点的情况,导致了滚动到底部时
// scrollHeight 减去滚动到底部时的scrollTop ,依然大于clientHeight 导致无法请求更多数据
// 这里将scrollTop向上取整 保证滚到底部时,触发调用
const CONDITION =
SELECTWRAP.scrollHeight -
Math.ceil(SELECTWRAP.scrollTop) <=
SELECTWRAP.clientHeight;
// el.scrollTop !== 0 当输入时,如果搜索结果很少,以至于没看到滚动条,那么此时的CONDITION计算结果是true,会执行bind.value(),此时不应该执行,否则搜索结果不匹配
if (CONDITION && SELECTWRAP.scrollTop !== 0) {
binding.value();
}
});
}
}
},
data() {
return {
keyword: "", // 存储关键字用
loading: false
};
},
methods: {
// 请求下一页的数据
loadMore() {
// 如果没有更多数据,则不请求
if (!this.hasMore) {
return;
}
// 如果intercept属性为true则不请求数据,
if (this.loadMore.intercept) {
return;
}
this.loadMore.intercept = true;
this.request({
page: this.page + 1,
more: true,
keyword: this.keyword
}).then(() => {
this.loadMore.intercept = false;
});
},
// 选中下拉框没有数据时,自动请求第一页的数据
focus() {
if (!this.data.length) {
this.request({
page: 1 });
}
},
handleSearch(keyword) {
this.keyword = keyword;
this.loading = true;
console.log(keyword);
this.request({
page: 1, keyword: keyword }).then(() => {
this.loading = false;
});
},
// 删除选中时,如果请求了关键字,则清除关键字再请求第一页的数据
clear() {
if (this.keyword) {
this.keyword = "";
this.request({
page: 1 });
}
}
}
};
</script>
参考文章:
el-select下拉加载
边栏推荐
- Li Kou's 300th weekly match
- Simulink and Arduino serial port communication
- LC weekly 300
- VB.net GIF(制作、拆解——优化代码,类库——5)
- BUU-Crypto-Cipher
- The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
- C language simple student management system (including source code)
- 小程序毕业设计---美食、菜谱小程序
- ping端口神器psping
- LC周赛300
猜你喜欢

2022 question bank and answers for safety management personnel of hazardous chemical business units

724. 寻找数组的中心下标

Zhanrui tankbang | jointly build, cooperate and win-win zhanrui core ecology

基于单片机的太阳能杀虫系统

BUU-Crypto-Cipher

Principle and practice of common defects in RSA encryption application

补某视频网站的js,进行视频解密

transformer坑了多少算力

LabVIEW错误对话框的出现

LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052
随机推荐
How to configure static IP for Kali virtual machine
Thread pool: use thread pool to optimize query speed
2022年A特种设备相关管理(电梯)考试题模拟考试平台操作
[matlab] general function of communication signal modulation - generation of narrow-band Gaussian white noise
PostgreSQL has officially surpassed mysql. Is this guy too strong!
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Yyds dry goods inventory TCP & UDP
Ping port artifact psping
[matlab] communication signal modulation general function - low pass filter
Graduation design of small programs -- small programs of food and recipes
LM small programmable controller software (based on CoDeSys) note XXI: error 3703
Etcd database source code analysis - initialization overview
2022 R2 mobile pressure vessel filling retraining question bank and answers
[interested reading] advantageous filtering modeling on long term user behavior sequences for click through rate pre
HMS v1.0 appointment.php editid参数 SQL注入漏洞(CVE-2022-25491)
BUU-Crypto-Cipher
Just do it with your hands 7 - * project construction details 2 - hook configuration
724. 寻找数组的中心下标
724. Find the central subscript of the array
With the advent of the IP era, how can E-sports hotels take advantage of the "east wind" of games?