实现el-table两列多选框且不可同时勾选

1、效果图如下,功能:必修和选修不可同时勾选
实现el-table两列多选框且不可同时勾选_第1张图片
2、代码如下

<template>
      <el-table :data="addTableData" style="width: 100%">
        <el-table-column label="必修" width="55px" align="center">
          <template slot-scope="scope">
            <el-checkbox v-model="checked1" :label="scope.row.id" @change="checkboxChange($event,scope.row.id,'checked2')"><span></span></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column label="选修" width="55px" align="center">
          <template slot-scope="scope">
            <el-checkbox v-model="checked2" :label="scope.row.id" @change="checkboxChange($event,scope.row.id,'checked1')"><span></span></el-checkbox>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="编号" width="60px" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" align="center"></el-table-column>
        <el-table-column prop="name" label="学时" width="60px"  align="center"></el-table-column>
      </el-table>
</template>


  export default {
    data() {
      return {
        checked1:[],//必修已选
        checked2:[],//选修已选
      }
    }

    methods: {
      checkboxChange(val,id,type) {
        if(val === true) {
          this[type] = this[type].filter(function(item) {
            return item !== id;  // 过滤出值不为2的元素
          });
        }
      },
    }}

你可能感兴趣的:(elementui)