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>
相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code3 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luoluoal4 小时前
基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
vue.js·spring boot·宠物
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome