Bootstrap

for循环多个el-form-item el-select组件,公用一份options数据,每个el-select组件选中后其他el-select组件禁用选中option项

主要逻辑就是利用属性  :disabled 进行判断是否禁用选择

<template>
  <div class="wrapper">
    <el-form ref="form" :model="form" label-width="120px" label-position="left" label-suffix=":">
      <div v-for="(item, index) in form.infoList" :key="index">
        <el-select v-model="item.level" @change="selectChange(index)">
          <el-option :disabled="getDisabled(option.value, index)" v-for="option in optionList" :key="option.value"
            :value="option.value" :label="option.label"></el-option>
        </el-select>
        <el-button @click="add">新增</el-button>
        <el-button @click="del(index)">删除</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      form: {
        infoList: [
          {
            level: ''
          }
        ],
      },
      selectValues: [],
      optionList: [
        {
          label: '1级',
          value: '1'
        },
        {
          label: '2级',
          value: '2'
        }
        {
          label: '3级',
          value: '3'
        }
        {
          label: '4级',
          value: '4'
        }
      ],
    };
  },
  computed: {},
  methods: {
    add() {
      this.form.infoList.push({
        level: ''
      })
    },

    del(index) {
      this.form.infoList.splice(index, 1)

      // 删除后重新赋值 this.selectValues触发判断, 恢复option项
      this.selectValues = this.form.infoList.map(item => item.level)
    },

    selectChange(index) {
      this.selectValues = this.form.infoList.map(item => item.level)
    },

    // 主要禁用的逻辑  例如第一个选择器选择1级后  后面的选择器禁止选择1级
    getDisabled(val, index) {
      return this.selectValues.some((selectValue, i) => {
        return i !== index && selectValue === val
      })
    }
  },
  mounted() { }
};
</script>
<style scoped></style>

;