el-select 支持多选 搜索远程数据 组件抽取

el-select 支持多选 搜索远程数据 组件抽取

  • 使用方式
js 复制代码
import selectView from './components/selectView'

<el-form>
  <el-form-item label="选择器">
    <selectView v-model="selValue" @change="handleChange">
  </el-form-item>
</el-form>
  • 组件
html 复制代码
 <template>
	<el-select
		v-model="selectValue"
		:multiple="multiple"
		:filterable="true"
		:remote="true"
		@focus="selectFocus"
		:clearable="true"
		:placeholder="placeholder"
		:remote-method="remoteMethod"
		:loading="selectLoading"
		@change="handleChange"
		style="width: 100%;"
	>
		<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
	</el-select>
</template>
<script>
import { userListAll } from "@/utils/api.js";
export default {
	props: {
		// v-model绑定 返回 1,2,3 或 传入 1,2,3 id值
		value: {
			type: [String, Number],
			default: "",
		},
		placeholder: {
			type: String,
			default: "请选择",
		},
		multiple: {
			type: Boolean,
			default: true,
		},
	},
	computed: {
		currentValue: {
			get: function() {
				if (this.value) {
					return this.value.split(",");
				}
				return [];
			},
			set: function(val) {
				this.$emit("input", val.join(","));
			},
		},
	},

	data() {
		return {
			selectValue: "",
			options: [], //存储下拉框的数据
			selectEnterpriseForm: {
				nickName: "",
			},
			selectLoading: false,
		};
	},

	mounted() {
		this.selectEnterprise("");
	},

	methods: {
		selectEnterprise(query) {
			//query用户搜索的值
			this.selectEnterpriseForm = this.$options.data().selectEnterpriseForm; //清空数据
			this.selectEnterpriseForm.nickName = query;

			userListAll({
				...this.selectEnterpriseForm,
			})
				.then(res => {
					this.options = [];
					this.selectLoading = false;
					this.addLoading = false;
					res.data.forEach(element => {
						this.options.push({
							value: element.userId + "",
							label: element.nickName,
						});
					});

					if (this.currentValue.length > 0) {
						this.selectValue = this.currentValue;
					}
				})
				.catch(err => {});
		},
		remoteMethod(query) {
			this.selectLoading = true;
			this.selectEnterprise(query);
		},
		selectFocus() {
			this.options = [];
			this.selectLoading = true;
			this.selectEnterprise("");
		},
		handleChange(val) {
			this.currentValue = val;
			let nameList = [];
			val.forEach(item => {
				this.options.forEach(element => {
					if (item == element.value) {
						nameList.push(element.label);
					}
				});
			});
			// 将1,2,3 和 张三,李四,王五 返回
			this.$emit("change", val.join(","), nameList.join(","));
		},
	},
};
</script>
<style lang="scss" scoped>
// .con {
// 	display: inline-block;
// 	width: 100%;
// }
</style>
相关推荐
风骏时光牛马1 小时前
生成一个AI多模态标题,仅输出一条标题,无额外文字、问句、备选、配图、文案等任何附加内容,不需要其他的任务服务 只要标题。
前端
wangruofeng2 小时前
从 GIF 到 AVIF:网页图片格式演进与 2026 年选型指南
前端·架构
清晨Coding3 小时前
为什么用 React Developer Tools 查看组件树这么乱?
前端·react.js
johnjincaaaa3 小时前
猿人学第 18 题「jsvmp 洞察先机」逆向通关实录
javascript·后端
鶴哥只手遮天3 小时前
《Electron 45.2.3 内核解剖》第 4 章 补丁体系:380 个补丁改了什么
前端·electron
FungLeo5 小时前
成为全栈·Vue 管理后台篇·分类树与标签管理:层级数据如何落到管理界面
前端·javascript·vue.js·分类树·naive ui·标签管理·前端数据结构
总要学点什么6 小时前
第108篇:三维特效-图片轨迹线效果——把贴图滚成一串箭头,想用它的本色反而整条消失
开发语言·javascript·ecmascript·gis·webgl·材质·cesium
Lank_M9 小时前
白底都占八成,PNG 为何只在图表页赢过 JPG
前端
张一凡939 小时前
给 Coding Agent 加一道"架构闸门":Jin 是如何做到"先审架构、再写代码、不漂移"的
前端·后端·agent
Linye9 小时前
AI 写的 HTML 原型全是空壳?聊聊真实 DOM 交互避坑
前端·人工智能