Element UI Select选择器实现多个下拉框中的一个选择某项后其余下拉框无法选择(完善版本)

效果预览

Element UI Select选择器实现多个下拉框中的一个选择某项后其余下拉框无法选择效果预览

源码示例

<template>
    <el-table :data="tableData" border stripe style="width: 100%" empty-text="暂无城市亲戚数量">
        <el-table-column prop="date" label="城市(多选)">
            <template slot-scope="scope">
                <el-select v-model="scope.row.citys" multiple placeholder="请选择城市" @change="changeSelect">
                    <el-option v-for="(item,key) in areas" :key="key" :label="item.value" :value="item.value" :disabled="!scope.row.citys.includes(item.value) && publicAreas.includes(item.value)"></el-option>
                </el-select>
                <input type="hidden" :name="'areas_dispatchs[' + scope.$index + '][citys][]'" v-model="item" v-for="item in scope.row.citys">
            </template>
        </el-table-column>
        <el-table-column label="亲戚数量" width="180">
            <template slot-scope="scope">
                <el-input v-model="scope.row.people_num" placeholder="请填写亲戚数量"></el-input>
                <input type="hidden" :name="'areas_dispatchs[' + scope.$index + '][people_num]'" v-model="scope.row.people_num">
            </template>
        </el-table-column>
        <el-table-column align="right" width="80">
            <template slot="header" slot-scope="scope">
                <el-button size="small" @click="addTable">添加</el-button>
            </template>
            <template slot-scope="scope">
                <el-button type="danger" size="small" @click="deleteTable(scope.$index)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>
export default {
    data(){
        return {
            //可选项
            areas: [],
            //公共选项列表
            publicAreas: [],
            //表格初始数据
            tableData: [
                {citys: ["长沙市","湘潭市"],people_num: '10'},
                {citys: ["邵阳市","衡阳市"],people_num: '12'}
            ]
        }
    },
    created() {
        /* 初始化可选项 */
        let areas = [],
            lev = ["长沙市","湘潭市","株洲市","衡阳市","邵阳市","郴州市","广州市","东莞市"];
        for(let i in lev){
            areas.push({ value: lev[i], disabled: false })
        }
        this.areas = areas;
        /* 初始化表格数据,得到公共选项列表'publicAreas' */
        if(this.tableData.length > 0){
            let publicArr = [];
            for(let m in this.tableData){
                publicArr = [...publicArr, ...this.tableData[m].citys]
            }
            this.publicAreas = publicArr;
        }
    },
    methods: {
        changeSelect(e){
            /* 监听下拉框值的变化,实时更新公共选项列表(选项的添加与删除) */
            let arr = [];
            for(let i in this.tableData){
                arr = [...arr, ...this.tableData[i].citys]
            }
            this.publicAreas = arr;
        },
        addTable(){
            /* 添加表格行 */
            this.tableData.push({citys: [],people_num: ''});
        },
        deleteTable(e){
            /* 删除表格某行,实时更新公共选项列表 */
            this.tableData.splice(e,1);
            let publicArr = [];
            for(let m in this.tableData){
                publicArr = [...publicArr, ...this.tableData[m].citys]
            }
            this.publicAreas = publicArr;
        }
    }
}

源码分析

关键代码 :disabled="!scope.row.citys.includes(item.value) && publicAreas.includes(item.value)"
解释:当前下拉框选中值存在于公共选项列表,但是不存在于当前选中列表时禁止选中

灵感来源于 原文链接
针对原文做出了一些完善,使之成为一个可以完整运行使用的demo

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
除了在 `el-option` 使用 `slot` 来自定义下拉框项的显示内容之外,还有其他几种方法可以实现下拉框某条数据显示星号: 1. 在 `el-option` 使用 `template` 来自定义下拉框项的显示内容: ```html <el-select v-model="value"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" > <template v-if="item.showStar"> <span>{{ item.label }}*</span> </template> <template v-else> <span>{{ item.label }}</span> </template> </el-option> </el-select> ``` 在上面的代码,我们在 `el-option` 使用了 `template` 来根据 `item.showStar` 的值来显示不同的内容。 2. 使用 `formatter` 属性来自定义下拉框项的显示内容: ```html <el-select v-model="value" :formatter="formatter"> <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" > </el-option> </el-select> ``` ```javascript methods: { formatter(row) { if (row.showStar) { return `${row.label}*` } else { return row.label } } } ``` 在上面的代码,我们使用了 `formatter` 属性来自定义下拉框项的显示内容,`formatter` 方法会接收一个参数 `row`,表示当前项的数据对象,我们可以根据 `row.showStar` 的值来显示不同的内容。 3. 使用插槽 `dropdown-item` 来自定义下拉框项的显示内容: ```html <el-select v-model="value"> <template v-for="item in options"> <el-option :key="item.value" :label="item.label" :value="item.value" > <template v-slot:dropdown-item> <span>{{ item.label }}</span> <span v-if="item.showStar">*</span> </template> </el-option> </template> </el-select> ``` 在上面的代码,我们使用了插槽 `dropdown-item` 来自定义下拉框项的显示内容,如果 `item.showStar` 的值为 `true`,则会在显示内容后面添加一个星号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值