ElementUI el-table多选框改为单选框的解决办法/插槽使用radio

需求:
表格能够单击数据行进行勾选且是单选的效果。

尝试了将type="selection"改成单选效果,但是还要把全选按钮隐藏掉。很麻烦!所以觉得比较好的解决方案是在插槽里使用el-radio

代码:

    <el-dialog title="历史记录查询" :visible.sync="historyDialog" width="800px" append-to-body>
      <el-button type="primary" plain size="mini" style="margin-bottom:10px;" :disabled="single" @click="updateRemark" >修 改</el-button>
      <div class="history-table">
        <el-table
          ref="table"
          :data="tableData"
          highlight-current-row
          :cell-style="tableCellStyle"
          height="400px"
          border
          @row-click="clickChange"
          :row-key="row => { return row.id }"
        >
          <el-table-column class-name="allow" label="选择" width="50" align="center"><template slot-scope="scope"><el-radio v-model="tableRadio" :label="scope.row" @click.native="clickChange"><i /></el-radio></template></el-table-column>
          <el-table-column  
            label="变动类型" 
            align="center"
            min-width='50'
            prop="changeTypeName" 
            :show-overflow-tooltip="true" >
          <el-table-column
            label="姓名"
            align="center"
            prop="userName"
            min-width='50'
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="更新时间"
            align="center"
            prop="changeTime"
            min-width='100'
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="信息"
            align="center"
            prop="message"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            :show-overflow-tooltip="true"
          />
        </el-table>
      </div>
      <!-- 分页 -->
    <el-pagination
      background
      layout="total, prev, pager, next"
      class="elp-class"
      :current-page="searchForm.currentPage"
      :page-sizes="[10, 20, 30, 40, 50]"
      :page-size="searchForm.size"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-dialog>
      tableRadio: [],
      single: true,
    // 表格选中
    clickChange(row) {
      if (this.tableRadio === row) {
        this.tableRadio = []
      } else {
        this.tableRadio = row
      }
      this.single = this.tableRadio?.id?.length ? false : true
    },
<style lang="scss" scoped>
.history-table{
  ::v-deep .el-table__body-wrapper{
    .el-radio__label {
      padding-left:0;
    }
  }
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以通过监听 el-table 的 @row-click 事件来实现 el-table 多选框单选功能。具体步骤如下: 1.在 el-table 中添加 type="selection",开启多选框功能。 2.在 el-table 中添加 @row-click="handleRowClick" 监听行点击事件。 3.在 methods 中添加 handleRowClick 方法,通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为选中项。 下面是示例代码: ```html <template> <el-table ref="table" :data="tableData" @row-click="handleRowClick" style="width: 100%"> <el-table-column type="selection" width="55"> </el-table-column> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [{ date: '2021-01-01', name: '张三', address: '北京市朝阳区' }, { date: '2021-01-02', name: '李四', address: '上海市浦东新区' }, { date: '2021-01-03', name: '王五', address: '广州市天河区' }], selection: [] } }, methods: { handleRowClick(row) { this.$refs.table.toggleRowSelection(row) this.selection = this.$refs.table.selection } } } </script> ``` 在上面的代码中,我们通过监听 @row-click 事件来实现了 el-table 多选框单选功能。在 handleRowClick 方法中,我们通过 $refs 获取到 el-table 的 selection 组件,然后将当前行的数据设置为选中项。最后,我们将选中的数据保存在 selection 变量中,以便后续使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值