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

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

  • 使用方式
import selectView from './components/selectView'

<el-form>
  <el-form-item label="选择器">
    <selectView v-model="selValue" @change="handleChange">
  </el-form-item>
</el-form>
  • 组件
 <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>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zsd_666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值