element-UI如何实现单选取消操作--Table控件与radio控件的组合操作

本文介绍了如何在Element-UI中通过将Table控件的触发事件改为row-click,来实现单选并能取消的操作。原先使用current-change事件无法实现取消选择,改用row-click事件后,问题得到解决,并附带了修改后的代码和效果图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

目录

无取消部分

解决方法: 修改table控件的触发事件 为行点击事件 row-click

效果图

参考链接


无取消部分

未实现取消操作的代码:

     <el-table
        v-loading="loadingg"
        :data="tableData" height="100%"
        style="width: 100%"
        :highlight-current-row="true"
        @current-change="clickChange">
        <el-table-column
          label="选择"
          width="50px">
          <template slot-scope="scope">
            <el-radio  v-model="currentInfo" :label="scope.row.id"><i></i></el-radio>
          </template>
        </el-table-column>

        <el-table-column
          prop="purchaseNo"
          min-width="160px"
          label="示例名称"  >
        </el-table-column >

      </el-table>

clickChange对应的事件:

methods: {
      clickChange (item) {
        // 绑定选中的数据
        this.currentInfo = item.id
        // ...
        // ...
      },
      // ...
}

使用的table事件是:  current-change

element-ui文档对该事件的介绍:

 

解决方法: 修改table控件的触发事件 为行点击事件 row-click

修改后的代码:

     <el-table
        v-loading="loadingg"
        :data="tableData" height="100%"
        style="width: 100%"
        :highlight-current-row="true"
        @row-click="clickItem">
        <el-table-column
          label="选择"
          width="50px">
          <template slot-scope="scope">
            <el-radio  v-model="currentInfo" :label="scope.row.id"><i></i></el-radio>
          </template>
        </el-table-column>

        <el-table-column
          prop="purchaseNo"
          min-width="160px"
          label="示例名称"  >
        </el-table-column >

      </el-table>

触发的事件:

methods: {
    clickItem (row, event, column) {
        // 先在data里定义好forbidden为true
        if (!this.forbidden) {
        this.forbidden = true
        return
    }
    if (this.currentInfo) {
        if (this.currentInfo == row.id) {
          this.currentInfo = ''
          //行点击后的 继续触发radio点击,禁止
          this.forbidden = false
          setTimeout(() => {
            this.forbidden = true
          }, 0)
          return
        } else {
          this.forbidden = false
          setTimeout(() => {
            this.forbidden = true
          }, 0)
        }
    } else {
        this.forbidden = false
        setTimeout(() => {
          this.forbidden = true
        }, 0)
    }
    // 绑定选中的数据
    this.currentInfo = row.id
    },
      // ...
}
这里的代码已经兼容了ie11,如果是Chrome,不考虑ie11,可以简化点:
methods: {
    clickItem (row, event, column) {
        if (!this.forbidden) {
        this.forbidden = true
        return
    }
    if (this.currentInfo) {
        if (this.currentInfo == row.id) {
          this.currentInfo = ''
          //行点击后的 继续触发radio点击,禁止
          this.forbidden = false
          setTimeout(() => {
            this.forbidden = true
          }, 0)
          return
    }
    // 绑定选中的数据
    this.currentInfo = row.id
    },
      // ...
}

效果图

 

参考链接

 https://blog.youkuaiyun.com/qq_38374502/article/details/81034552#commentBox

 

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值