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;
			}
		});
	}
}
相关推荐
ai产品老杨27 分钟前
减少交通拥堵、提高效率、改善交通安全的智慧交通开源了。
前端·vue.js·算法·ecmascript·音视频
lexiangqicheng32 分钟前
JS-- for...in和for...of
开发语言·前端·javascript
smallluan36 分钟前
JS设计模式(4):观察者模式
javascript·观察者模式·设计模式
粥里有勺糖1 小时前
视野修炼-技术周刊第122期 | 发光图片制作
前端·javascript·github
y102121041 小时前
Python训练营打卡Day42
开发语言·javascript·ecmascript
Carlos_sam1 小时前
OpenLayers:封装Tooltip
前端·javascript
张老爷子2 小时前
记录uniapp开发安卓使用webRTC实现语音推送
vue.js
工呈士2 小时前
MobX与响应式编程实践
前端·react.js·面试
嘉小华2 小时前
Android Lifecycle 使用
前端
Sherry0072 小时前
实时数据传输协议:WebSocket vs MQTT
前端·websocket