【sgAutocomplete】自定义组件:基于elementUI的el-autocomplete组件开发的自动补全下拉框组件(带输入建议的自动补全输入框)

d4f50ad8f5e54f549e447d0a0a6c694f.gif

特性:

1、支持本地保存选中过的记录

2、支持动态接口获取匹配下拉框内容

3、可以指定对应的显示label和字段组件key

4、自动生成速记符字段(包含声母和全拼两种类型),增强搜索匹配效率

sgAutocomplete源码

<template><!-- 基于elementUIel-autocomplete组件开发的自动补全下拉框组件 --><el-autocomplete:class="$options.name"style="width: 100%"ref="autocomplete":popper-class="'sgAutocomplete-el-autocomplete'"v-model="inputSearchValue":placeholder="placeholder || `输入关键词…`":value-key="valueKey || `label`":fetch-suggestions="fetchSuggestions":hide-loading="false"@focus="$refs.autocomplete.$el.querySelector('input').select()"@select="selectSuggestionsItem"@clear="focusAutocomplete":debounce="0"clearable><template slot-scope="{ item }"><div><i v-if="item.isHistory" class="history-icon el-icon-time" /><span class="label">{{ item[labelKey || `label`] }}</span></div></template><!-- 搜索按钮1 --><i class="el-icon-search el-input__icon" slot="suffix" v-if="showSearchButton == 1" /><!-- 删除历史记录按钮 --><i:title="clearHistoryTitle || `删除历史记录`"class="el-icon-delete el-input__icon clearHistory"slot="suffix"v-if="showHistoryBtn"@click="clearHistory"/><!-- 搜索按钮2 --><el-buttonslot="append"icon="el-icon-search"@click="focusAutocomplete"v-if="showSearchButton == 2"></el-button></el-autocomplete>
</template><script>
import pinyin from "@/js/pinyin";
export default {name: "sgAutocomplete",components: {},data() {return {inputSearchValue: null,historyListLocalStorageName: null, //保存到本地记录的localStorage KeysearchItems: [],showHistoryBtn: false,};},props: ["data", //可选项数组(必选参数)"value","valueKey", //获取值"labelKey", //显示值"placeholder","clearHistoryTitle", //删除历史记录按钮提示"filterKeys", //匹配搜索的字段(数组)不传此参数默认就用labelKey"showHistory", //显示历史选择记录"showSearchButton", //显示搜索按钮(样式:1 是在输入框里面的icon,2 是在输入框后面的按钮)"autofocus",],watch: {data: {handler(newValue, oldValue) {if (newValue && Object.keys(newValue).length) {this.searchItems = JSON.parse(JSON.stringify(newValue));this.searchItems.forEach((v) => {v.SJF = pinyin.getCamelChars(v[this.labelKey || "label"]); //速记符(声母)v.SJF_full = pinyin.getFullChars(v[this.labelKey || "label"]); //速记符(全拼)});}},deep: true, //深度监听immediate: true, //立即执行},value: {handler(newValue, oldValue) {this.inputSearchValue = newValue;},deep: true, //深度监听immediate: true, //立即执行},inputSearchValue: {handler(newValue, oldValue) {this.$emit(`input`, newValue);},deep: true, //深度监听immediate: true, //立即执行},showHistory: {handler(newValue, oldValue) {this.historyListLocalStorageName = newValue;},deep: true, //深度监听immediate: true, //立即执行},},mounted() {(this.autofocus === "" || this.autofocus) && this.focusAutocomplete(); //默认聚焦},methods: {focusAutocomplete(d) {this.$nextTick(() => {this.$refs.autocomplete.focus();this.$refs.autocomplete.activated = true; //这句话是重点});},// 搜索下拉框fetchSuggestions(queryString, callback) {if (queryString) {queryString = queryString.toString().trim();let r = this.searchItems.filter((v, i, ar) => {let filterKeys = this.filterKeys || [this.labelKey];filterKeys.push("SJF", "SJF_full"); //自动匹配声母、全屏组合return filterKeys.some((filterKey) =>v[filterKey].toLocaleLowerCase().includes(queryString.toLocaleLowerCase()));});this.showHistoryBtn = false;callback(r);} else {let historys = this.getHistorys();historys.forEach((v) => (v.isHistory = true)); //标识是历史记录this.showHistoryBtn = historys.length > 0;callback(historys);}},selectSuggestionsItem(d) {let historys = this.getHistorys();if (historys.length) {let k = this.valueKey || this.labelKey || "label";let has = historys.some((v) => v[k] == d[k]);has || historys.unshift(d);localStorage[this.historyListLocalStorageName] = JSON.stringify(historys);} else {localStorage[this.historyListLocalStorageName] = JSON.stringify([d]);}this.$emit(`change`, d);},getHistorys() {let historys = localStorage[this.historyListLocalStorageName];return JSON.parse(historys || "[]");},clearHistory(d) {delete localStorage[this.historyListLocalStorageName];this.showHistoryBtn = false;this.focusAutocomplete();},},
};
</script><style lang="scss" scoped>
.sgAutocomplete {.clearHistory {cursor: pointer;&:hover {color: #409eff;}}
}
</style>

里面用到的pinyin.js在这篇文章里面JS自动生成速记符、拼音简写/拼音的声母(例如:“你挚爱的强哥”转换为“NZADQG”)。提取首字母,返回大写形式;提取拼音, 返回首字母大写形式(全拼)。_你挚爱的强哥的博客-CSDN博客文章浏览阅读2.7k次。需要引用以下pinyin.js文件。https://blog.csdn.net/qq_37860634/article/details/130765296

用例

<template><div><sgAutocompleteautofocusv-model="sgAutocompleteValue":data="data":placeholder="`输入搜索关键词...`":valueKey="`value`":labelKey="`label`"showHistory="localStorageHistoryName"showSearchButton="2"@change="changeSgAutocomplete"/><p style="margin-top: 20px">选择的数据:{{ sgAutocompleteValue }}</p><pstyle="margin-top: 20px;word-wrap: break-word;word-break: break-all;white-space: break-spaces;"><span>选择的对象:</span>{{ sgAutocompleteObject ? JSON.stringify(sgAutocompleteObject, null, 2) : "" }}</p></div>
</template>
<script>
import sgAutocomplete from "@/vue/components/admin/sgAutocomplete";
export default {components: { sgAutocomplete },data() {return {sgAutocompleteValue: null,sgAutocompleteObject: null,data: [],//模拟数据1dataA: [{ value: "1", label: "A显示文本1" },{ value: "2", label: "A显示文本2" },{ value: "3", label: "A显示文本3" },{ value: "4", label: "A显示文本4" },{ value: "5", label: "A显示文本5" },],//模拟数据2dataB: [{ value: "1", label: "B显示文本1" },{ value: "2", label: "B显示文本2" },{ value: "3", label: "B显示文本3" },{ value: "4", label: "B显示文本4" },{ value: "5", label: "B显示文本5" },],};},watch: {// 模拟动态更新筛选项sgAutocompleteValue: {handler(newValue, oldValue) {if (newValue && Object.keys(newValue).length) {switch (newValue.toLocaleLowerCase()) {case "a":this.data = this.dataA;break;case "b":this.data = this.dataB;break;}}},},},methods: {changeSgAutocomplete(d) {this.sgAutocompleteObject = d;},},
};
</script>

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/264695.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STM32F103时钟树

STM32芯片时钟来源 RCC时钟树简图 RCC时钟树详细图 1&#xff0e;当HSI被用于作为PLL时钟的输入时&#xff0c;系统时钟能得到的最大频率是64MHz。 2&#xff0e;用户可通过多个预分频器配置AHB、高速APB(APB2)和低速APB(APB1)域的频率。AHB和 APB2域的最大频率是72MHz。APB1域…

智能优化算法应用:基于樽海鞘群算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于樽海鞘群算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于樽海鞘群算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.樽海鞘群算法4.实验参数设定5.算法结果6.…

Flutter自定义下拉选择框dropDownMenu

利用PopupMenuButton和PopupMenuItem写了个下拉选择框&#xff0c;之所以不采用系统的&#xff0c;是因为自定义的更能适配项目需求&#xff0c;话不多说&#xff0c;直接看效果 下面直接贴出代码、代码中注释写的都很清楚&#xff0c;使用起来应该很方便&#xff0c;如果有任何…

Diffusion Models: A Comprehensive Survey of Methods and Applications

摘要 扩散模型作为一个强大的新的深度生成模型系列出现&#xff0c;在许多应用中具有破纪录的性能&#xff0c;包括图像合成、视频生成和分子设计。在这项调查中&#xff0c;我们对迅速扩大的扩散模型的工作进行了概述&#xff0c;将研究分为三个关键领域&#xff1a;有效采样…

[b01lers2020]Life on Mars 一个接口的sql schema.schemate表

这里还是很简单的 啥也没有 然后抓包看看 发现传递参数 直接尝试sql 然后如果正确就会返回值 否则 返回1 chryse_planitia union select database(),version() 发现回显 直接开始注入 chryse_planitia union select database(),version()chryse_planitia union select data…

LeetCode5.最长回文子串

昨天和之前打比赛的队友聊天&#xff0c;他说他面百度面到这道算法题&#xff0c;然后他用暴力法解的&#xff0c;面试官让他优化他没优化出来&#xff0c;这道题我之前没写过&#xff0c;我就想看看我能不能用效率高一点的方法把它做出来&#xff0c;我一开始就在想用递归或者…

使用dockerfile 构建自己的nacos-mysql

前言 在部署nacos的时候触发的脑袋灵光一闪&#xff0c;每次部署nacos都要部署下mysql服务器&#xff0c;然后导入sql语句&#xff0c;配置nacos配置文件&#xff0c;那有没有简单的方法实现一键部署nacos和nacos-mysql 呢? 答案是肯定&#xff01;如下目录图&#xff1a; …

C++笔记之Delegate和委托构造(Delegating constructor)

C笔记之Delegate和委托构造辨析 code review! —— 杭州 2023-12-10 文章目录 C笔记之Delegate和委托构造辨析0.有道词典&#xff1a;英语发音1.ChatGPT&#xff1a;delegate概念详解2.Delegate和“将可调用对象作为函数参数”是不是一回事&#xff1f;3.C的Delegate示例4.…

二维码智慧门牌管理系统升级解决方案:数字化房产管理

文章目录 前言一、全面信息记录&#xff1a;提升管理效率二、多种优势功能&#xff1a;系统化管理与无缝对接三、安全隐私保护&#xff1a;数据安全的重要性四、总结&#xff1a;提升管理效率与居住体验 前言 科技驱动房产管理 随着科技的飞速发展&#xff0c;房产管理领域也面…

力扣每日一题day33[111. 二叉树的最小深度]

给定一个二叉树&#xff0c;找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明&#xff1a;叶子节点是指没有子节点的节点。 示例 1&#xff1a; 输入&#xff1a;root [3,9,20,null,null,15,7] 输出&#xff1a;2示例 2&#xff1a; 输入…

Day56力扣打卡

打卡记录 数对统计&#xff08;DP状态压缩&#xff09; 参考文献 #include <bits/stdc.h>using namespace std;void solve(){int n;cin >> n;map<int, int> mapp;vector<int> a(n);for (auto& x : a){cin >> x;mapp[x] ;}vector<array&…

探索HarmonyOS_开发软件安装

随着华为推出HarmonyOS NEXT 宣布将要全面启用鸿蒙原声应用&#xff0c;不在兼容安卓应用&#xff0c; 现在开始探索鸿蒙原生应用的开发。 HarmonyOS应用开发官网 - 华为HarmonyOS打造全场景新服务 鸿蒙官网 开发软件肯定要从这里下载 第一个为微软系统(windows)&#xff0c;第…