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>
相关推荐
EndingCoder5 分钟前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客1 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro2 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom2 小时前
javaweb -html -CSS
前端·javascript·html
CodeCraft Studio2 小时前
【案例分享】如何借助JS UI组件库DHTMLX Suite构建高效物联网IIoT平台
javascript·物联网·ui
打小就很皮...3 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡4 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜054 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx5 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9995 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序