el-table 设置selection多选,实现单选功能

需求是使用el-table的多选功能,然后不想要多选改成单选。
在这里插入图片描述
代码

<template>
  <div class="contentBox"
       v-loading="loading">
 <el-table :data="list"
                ref="accountRef"
                @select="handleTableChange">
        <el-table-column type="selection"  width="45"></el-table-column>
        <el-table-column width="100" prop="name"></el-table-column>
        <el-table-column width="100" prop="no"></el-table-column>
        <el-table-column width="100" prop="date"></el-table-column>
   </el-table>     
   </div>
   </template>                 
<script>
import {
  defineAsyncComponent,
  defineComponent,
  getCurrentInstance,
  onMounted,
  reactive,
  toRefs,
} from "vue";

export default defineComponent({
  components: { },
  setup(props, context) {
    const { proxy } = getCurrentInstance();
    const accountRef = ref(null)
    const data = reactive({
      list: [],
      selectTable: [],
    });
    //表格单选
    const handleTableChange = (list, row) => {
      if (list.length === 0) {
        data.selectTable = [];
        return
      }
      // let itemData = JSON.parse(JSON.stringify(row))
      accountRef.value.clearSelection()
      data.list.forEach((ele) => {
        if (ele.id === row.id) {
          accountRef.value.toggleRowSelection(ele, true)
          data.selectTable = [ele];
        }
      })
    };
    return {
      ...toRefs(data),
      handleTableChange,
      accountRef
    };
  },
});
</script>

这里是vue3的写法
1、绑定table实例ref=“accountRef”,然后记的声明const accountRef = ref(null)和return返回里加上accountRef。
2、绑定select方法,定义handleTableChange函数 @select=“handleTableChange”
3、table的绑定事件select,select有两个回掉参数selection, row。selection选中的数据数组,row当前选中的数据。
4、table的方法,clearSelection()清除表格选中,toggleRowSelection(ele, true) 手动选中。先取清除选中,再根据勾选你选择的那一条数据。
5、 toggleRowSelection有两个参数一个是要选的数据,一个是该数据的状态,true选中,false不选

可以通过监听 el-table 的 @row-click 事件来实现 el-table 多选框的单选功能。具体步骤如下: 1.在 el-table 中添加 type="selection",开启多选功能。 2.在 el-table 中添加 @row-click="handleRowClick" 监听行点击事件。 3.在 methods 中添加 handleRowClick 方法,通过 $refs 获取到 el-tableselection 组件,然后将当前行的数据设置为选中项。 下面是示例代码: ```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-tableselection 组件,然后将当前行的数据设置为选中项。最后,我们将选中的数据保存在 selection 变量中,以便后续使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值