记录element-ui改造select显示为table,并支持多查询条件

最近遇到的一个需求 , 很有趣,是需要一个select组件,要求显示工号,员工姓名,以及区域

三个字段,并且要支持三个字段的查询。显然element原生的组件不适用,这时候我们需要改造一下,把option改成一个table的样子,这样就能解决我们的问题 , 多个搜索条件这里我是一次性拿到所有的数据,然后模糊查询来解决

1.先看效果图 点击select支持输入,选择,以及回车查询

2.实现代码 HTML

html 复制代码
 <el-select v-model="value" clearable filterable :filter-method='filterMethod' placeholder="请选择">
   <el-option disabled value="disabled ">
		<div class='saler-mate-list-item disabled-title'>
			<span class='code'>工号</span>
			<span class='name'>姓名</span>
			<span class='spec'>区域</span>
		</div>
	</el-option>

	<template v-if="options && options.length">
	    <el-option
	      v-for="item in options"
	      :key="item.value"
	      :label="item.label"
	      :value="item.value">
			<div class='saler-mate-list-item disabled-title'>
				<span class='code'>{{item.code}}</span>
				<span class='name'>{{item.label}}</span>
				<span class='spec'>{{item.spec}}</span>
			</div>
		</el-option>
	</template>
	<el-option v-else disabled>暂无数据</el-option>
  </el-select>

3. 对应Css

css 复制代码
.saler-mate-list-item{
	display: flex;
 	justify-content: space-between;
	align-items:center;
	width:500px;
	padding-left:20px;
}

.name {
	width:45%;
	text-align:left;
	wite-space:nowrap;
	text-overflow:elipsis;
}

.code{
	width:30%;
	text-align:left;
	wite-space:nowrap;
	text-overflow:elipsis;
}

.spec {
	width:25%;
	text-align:left;
	wite-space:nowrap;
	text-overflow:elipsis;
}

4.对应JS

javascript 复制代码
filterMethod(val){
	if(val){
		// 不建议操作原数组
		let newArr = this.options.filter(i => {
		return i.codo.includes(val) || i.name.includes(val) || i.spec.includes(val)})
		this.options = JSON.parse(JSON.stringify(newArr))
	} else {
		// 搜索内容为空时 , 要把原始数据赛回select中
		this.options = this.dataList
	}
}
相关推荐
小兔子6 小时前
Python 的 GIL 与 free-threading:3.13 之后「去 GIL」走到哪一步了
前端
IT_陈寒6 小时前
SpringBoot自动配置差点让我加班到凌晨
前端·人工智能·后端
guslegend7 小时前
脚手架原理与本地调试:从 bin 软链接到 npm link
前端·npm·node.js·脚手架·前端工程化
小宋10217 小时前
A2UI从零实战:Agent安全生成交互界面与事件回传
javascript·人工智能·安全·交互
大包子7 小时前
structuredClone vs lodash.cloneDeep:核心差异与选型指南
javascript·vue.js
海码事务所7 小时前
Google Play 新个人开发者账号上架指南:12 人连续 14 天封闭测试怎么做?
前端
田威AI7 小时前
图片内文字翻译的规格:输入输出、保真、自动化、时间与费用
前端·计算机视觉
不可能片场8 小时前
命令行中文变问号 我用环境变量救了场
前端·electron
lerhxx8 小时前
AI 应用中的上下文管理 —— 从"上下文窗口"到"分层压缩"的工程实践
前端·javascript
骑着蜗牛撵大象3278 小时前
多 Agent 分治协作:MapReduce 模式下的并行扇出与结果归并
前端