当前位置:网站首页>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下拉加载
边栏推荐
- Zzulioj:1201: mode problem
- 空洞卷积、可变形卷积、可变形ROI Pooling
- Signification des lettres du module optique et abréviation des paramètres Daquan
- Actual cases and optimization solutions of cloud native architecture
- Descriptive analysis of data distribution characteristics (data exploration)
- Trie number dictionary tree
- C language simple student management system (including source code)
- Introduction to AMBA
- Wechat applet +php realizes authorized login
- 小程序毕业设计---美食、菜谱小程序
猜你喜欢

2022 a special equipment related management (elevator) examination questions simulation examination platform operation

BUU-Crypto-Cipher

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

SQL injection - injection based on MSSQL (SQL Server)

Enterprise level log analysis system elk (if things backfire, there must be other arrangements)

C language simple student management system (including source code)

transformer坑了多少算力

Automated testing selenium foundation -- webdriverapi

ping端口神器psping

Analysis of classical pointer and array written test questions in C language
随机推荐
Notepad++ -- display related configurations
[matlab] matlab simulates digital bandpass transmission systems - QPSK and OQPSK systems
Flask
Just do it with your hands 7 - * project construction details 2 - hook configuration
Use of hutool Pinyin tool
TCP state transition diagram
Unity is connected to the weather system
LM small programmable controller software (based on CoDeSys) note 22: error 4268/4052
Supplement the JS of a video website to decrypt the video
Just do it with your hands 7 - * project construction details 2 - hook configuration
Nodejs learning document
VB.net GIF(制作、拆解——优化代码,类库——5)
Accidentally deleted the data file of Clickhouse, can it be restored?
Daily question brushing record (12)
[paper summary] zero shot semantic segmentation
[matlab] matlab simulates digital bandpass transmission system ask, PSK, FSK system
The data mark is a piece of fat meat, and it is not only China Manfu technology that focuses on this meat
One click filtering to select Baidu online disk files
Appearance of LabVIEW error dialog box
Talk about the SQL server version of DTM sub transaction barrier function