ElementUI2.x El-Select组件 处理使用远程查找时下拉箭头丢失问题

devtools/2024/10/20 6:54:56/

1、问题描述

elementui2.x版本使用el-select组件的remote-method远程查找时,发现下拉箭头丢失了,且当查找接口返回数据为空时,也不会展开下拉列表来显示暂无数据提示;

2、源码解析

官网使用示例:

<template><el-selectv-model="value"multiplefilterableremotereserve-keywordplaceholder="请输入关键词":remote-method="remoteMethod":loading="loading"><el-optionv-for="item in options":key="item.value":label="item.label":value="item.value"></el-option></el-select>
</template>

ElSelect组件源码问题解析:

1、查看源码发现是当props属性remote filterable 都为true时,导致计算属性iconClass返回的是空字符,所以下拉箭头不显示;

computed:{...前面代码省略iconClass() {return this.remote && this.filterable ? '' : (this.visible ? 'arrow-up is-reverse' : 'arrow-up');},...后面代码省略
}

2、分析下面代码,发现下拉框只有在emptyText不为falsevisibletrue的情况下才会显示,且empty部分也必须emptyText有值才会显示,所以得去找emptyText

<!-- 前面代码省略 -->
<transitionname="el-zoom-in-top"@before-enter="handleMenuEnter"@after-leave="doDestroy"><el-select-menuref="popper":append-to-body="popperAppendToBody"v-show="visible && emptyText !== false"><el-scrollbartag="ul"wrap-class="el-select-dropdown__wrap"view-class="el-select-dropdown__list"ref="scrollbar":class="{ 'is-empty': !allowCreate && query && filteredOptionsCount === 0 }"v-show="options.length > 0 && !loading"><el-option:value="query"createdv-if="showNewOption"></el-option><slot></slot></el-scrollbar><template v-if="emptyText && (!allowCreate || loading || (allowCreate && options.length === 0 ))"><slot name="empty" v-if="$slots.empty"></slot><p class="el-select-dropdown__empty" v-else>{{ emptyText }}</p></template></el-select-menu></transition></div>
</template>

3、找到emptyText代码分析发现,当remotetrue,且query为空,options数组长度为0时,返回的是false,也就导致为什么当远程查询无数据时下拉框不显示的问题;

computed:{...前面代码省略emptyText() {if (this.loading) {return this.loadingText || this.t('el.select.loading');} else {if (this.remote && this.query === '' && this.options.length === 0) return false;if (this.filterable && this.query && this.options.length > 0 && this.filteredOptionsCount === 0) {return this.noMatchText || this.t('el.select.noMatch');}if (this.options.length === 0) {return this.noDataText || this.t('el.select.noData');}}return null;},...后面代码省略
}

 3、解决办法(不想看源码解析可直接跳转到这里)

创建一个js文件(例如select.js),目录位置自己定,里面内容如下:

export default {computed: {iconClass() {return this.visible ? 'arrow-up is-reverse' : 'arrow-up';},emptyText() {if (this.loading) {return this.loadingText || this.t('el.select.loading');} else {if (this.remote && this.query === '' && this.options.length === 0) {return this.noMatchText || this.t('el.select.noMatch');};if (this.filterable && this.query && this.options.length > 0 && this.filteredOptionsCount === 0) {return this.noMatchText || this.t('el.select.noMatch');}if (this.options.length === 0) {return this.noDataText || this.t('el.select.noData');}}return null;},}
}

main.js中导入并使用对象结构合并到ElementUI.Select.computed上,注意这里必须要用结构来覆盖原有值,使用mixins混入是无效的(亲测),且结构最好在Vue.use(ElementUI, { size: 'mini' })前进行;

import Vue from 'vue'
import ElementUI from 'element-ui';
import '@/style/element-variables.scss'import select_mixin from '@/components/elementui/mixins/select';
ElementUI.Select.computed = {...ElementUI.Select.computed, ...select_mixin.computed};
Vue.use(ElementUI, { size: 'mini' });

全篇完!


http://www.ppmy.cn/devtools/111304.html

相关文章

鱼类检测-目标检测数据集(包括VOC格式、YOLO格式)

鱼类检测-目标检测数据集&#xff08;包括VOC格式、YOLO格式&#xff09; 数据集&#xff1a; 链接&#xff1a;https://pan.baidu.com/s/1B4o8IgOmAWeQJDWpJWxqXg?pwdjaco 提取码&#xff1a;jaco 数据集信息介绍&#xff1a; 共有 2848 张图像和一一对应的标注文件 标注文…

机器学习:逻辑回归--过采样

目录 前言 一、为什么使用过采样&#xff1f; 二、代码实现 1.完整代码 2.数据预处理 3.进行过采样 4.建立模型 5.绘制混淆矩阵 总结 前言 过采样&#xff08;Oversampling&#xff09;是指在数据处理或机器学习中&#xff0c;增加少数类样本的数量以平衡类别分布。常…

图片详解,最简单易懂!!!Ubuntu增强功能

文章目录 共享粘贴板共享文件夹vim 共享粘贴板 双击 输入认证用户密码 显示这个界面 命令行界面 reboot就会重新启动 虚拟机 设置下面的选项&#xff0c;就可以实现共享粘贴 共享文件夹 先在本机&#xff08;windows&#xff09;新建一个共享文件夹 再在ubuntu建一个共…

Web日志分析工具GoAccess

目录 1. 介绍 2. 功能 3. 支持的格式 4. 安装 从发布版本构建 从GitHub构建&#xff08;开发&#xff09; 命令行安装 5. 使用 5.1 监视Apache日志 5.2 通过web仪表板查看日志 浏览器访问 5.3 汉化设置 测试访问 1. 介绍 GoAccess是一个开源的实时网络日志分析器和…

Axure设计之全屏与退出全屏交互实现

在Axure RP中&#xff0c;设计全屏与退出全屏的交互功能可以极大地提升用户体验&#xff0c;尤其是在展示产品原型或进行演示时。本文将详细介绍如何在Axure RP中通过结合JavaScript代码实现全屏与退出全屏的交互效果。 ​ Axure原型设计web端交互元件库&#xff1a;https://…

Java 常用集合方法详解

在 Java 编程中&#xff0c;集合框架提供了丰富的数据结构和算法来存储和操作数据。集合框架主要包含了 List、Set 和 Map 接口&#xff0c;其中 List 和 Map 是最常用的接口。本文将深入探讨 List 和 Map 接口下的集合类及其常用方法&#xff0c;包括实际应用示例和代码片段。…

如何看待2020年的最新国际视频编解码标准H.266?

技术背景与发布 H.266&#xff0c;也被称为多功能视频编码&#xff08;Versatile Video Coding&#xff0c;简称VVC&#xff09;&#xff0c;是2020年定稿并发布的最新一代国际视频编码标准。 发布时间与机构&#xff1a;H.266由ISO/IEC MPEG和ITU-T VCEG联合制定&#xff0c;…

计算机网络(Hub 集线器、交换机、路由器)

计算机网络 匹配符Hub 集线器实现多台电脑之间的通信&#xff1a;交换机实现两个局域网之间的通信&#xff1a;路由器 OSI参考模型TCP/IP协议族家庭宽带中&#xff1a; 无线路由器IP地址NAT 网络地址转换&#xff1a;解决IP地址不足 VPN 匹配符 Hub 集线器 群发 半双工 实现…