el-table实现单选(radio,checkbox多选改成单选)、多选,以及清空和回显

一、实现单选radio

1、table 部分

<el-table
    :data="tableData"
    @row-click = "showRow"
>
    // 添加column
    <el-table-column label="选择" width="50" align="center">
        <template scope="scope">
          <el-radio class="radio"  v-model="radio"  :label="scope.row.id">
              // 这里目的是为了实现单选
              &nbsp;
          </el-radio>
       </template>
    </el-table-column>
</el-table>

2、data部分

data:{
	tableData: [],
    // 单选(null清空状态)
	radio: null,
	selected:{}
},

3、method 部分

showRow(row){
    //赋值给radio
    this.radio = this.tableData.indexOf(row);
    this.selected=row;
},

清空状态设置radio = null;回显给radio赋值

二、实现checkbox多选改成单选

1、table 部分

<el-table
    :data="table"
     ref="table"
    @selection="handleSelection"
> 
    <el-table-column
      type="selection"
      align="center"
      width="55">
    </el-table-column>
</el-table>

2、data 部分

data:{
    table: [],
    row : null,
},

3、method 部分

    // 点击表格中的行
    handleSelection(selection, row) {
      // 取消之前的选择
      if (this.row) {
        this.$refs.table.toggleRowSelection(this.row, false);
      }
      this.row = row;
    },
    // 清空表格选中的项
    clearSelect() {
      if (this.row) {
        this.$refs.table.clearSelection();
      }
    },

清空状态设置row= null;回显给row赋值

4、css部分

// 隐藏表格的全选按钮
::v-deep.el-table {
  .el-table__header-wrapper {
    .el-table-column--selection {
      .el-checkbox {
        display: none;
      }
    }
  }
  .el-table__fixed {
    .el-table__fixed-header-wrapper {
      .el-table-column--selection {
        .el-checkbox {
          display: none;
        }
      }
    }
  }
}

三、实现多选

1、table 部分

<el-table
    :data="tableData"
     ref="multipleTable"
    @selection-change="handleSelectionChange"
> 
    <el-table-column
      type="selection"
      align="center"
      width="55">
    </el-table-column>
</el-table>

2、data 部分

data:{
    tableData: [],
    multipleSelection: []
},

3、method 部分

handleSelectionChange(val) {
      this.multipleSelection = val;
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值