el-select实现可复制一段“关键词“(多个)实现搜索 以及 回车选中搜索项

el-select实现可复制一段"关键词"(多个)实现搜索 以及 回车选中搜索项

html 复制代码
<el-select
	ref="productRef"
	filterable
	clearable
	v-model="fItem.productName"
	multiple
	:reserve-keyword="true"
	remote
	:filter-method="remoteMethod"
	@visible-change="visibleChange"
	@keyup.enter="handleEnterKey(fIndex)"
>
	<el-option
		v-for="(item3, index3) in portFullNameOpt"
		:key="index3"
		:label="(item3.productCode ? '(' + item3.productCode + ')' : '') + item3.productName"
		:value="item3.productId"
	></el-option>
</el-select>

添加remote属性,用filter-method实现自定义搜索方法

puppet 复制代码
// 多搜索
function remoteMethod(query: any) {
	if (query !== '') {
	// 当前场景query是以空格分开的关键词,比如"001  002",所以处理成数组形式
		state.keyUpData = query.split(' ');
		// 接口过滤
		selectAllProductInfo({ productName: query }).then((res: any) => {
			state.portFullNameOpt = res.data;
		});
	} else {
		state.portFullNameOpt = state.pordOriginalOpt;
	}
}
// 下拉框消失时,恢复默认选项
function visibleChange(data: any) {
	if (!data) {
		state.portFullNameOpt = state.pordOriginalOpt;
	}
}
// 回车自动选中搜索过滤出来的选项
function handleEnterKey(fIndex: number) {
	if (state.keyUpData.length) {
	// 场景:复制搜索的是关键词,但是绑定的是productId,所以处理一下
		const queryProductId = state.portFullNameOpt.filter((item1: { productCode: any }) => state.keyUpData.includes(item1.productCode)).map((item2: { productId: any }) => item2.productId);
		// console.log(queryProductId, 'att');
		state.newAgencySalesList.forEach((item: any, index: number) => {
			if (fIndex == index) {
				item.productName = queryProductId;
			}
		});
	}
}
相关推荐
王景程2 分钟前
如何测试短信接口
java·服务器·前端
安冬的码畜日常25 分钟前
【AI 加持下的 Python 编程实战 2_10】DIY 拓展:从扫雷小游戏开发再探问题分解与 AI 代码调试能力(中)
开发语言·前端·人工智能·ai·扫雷游戏·ai辅助编程·辅助编程
烛阴27 分钟前
Node.js中必备的中间件大全:提升性能、安全与开发效率的秘密武器
javascript·后端·express
小杨升级打怪中30 分钟前
前端面经-JS篇(三)--事件、性能优化、防抖与节流
前端·javascript·xss
清风细雨_林木木34 分钟前
Vue开发网站会有“#”原因是前端路由使用了 Hash 模式
前端·vue.js·哈希算法
鸿蒙布道师1 小时前
OpenAI为何觊觎Chrome?AI时代浏览器争夺战背后的深层逻辑
前端·人工智能·chrome·深度学习·opencv·自然语言处理·chatgpt
袈裟和尚1 小时前
如何在安卓平板上下载安装Google Chrome【轻松安装】
前端·chrome·电脑
曹牧1 小时前
HTML字符实体和转义字符串
前端·html
小希爸爸1 小时前
2、中医基础入门和养生
前端·后端
局外人LZ1 小时前
前端项目搭建集锦:vite、vue、react、antd、vant、ts、sass、eslint、prettier、浏览器扩展,开箱即用,附带项目搭建教程
前端·vue.js·react.js