el-table表格多选改为单选

在项目中,经常会有表格展示,前端通常用的是el-table表格,该组件提供的选择为多选,但是当需求要求表格只允许单选怎么写呢?下面就将如何把多选改为单选详细告诉你们~

1. 运用el-table

select-all 代表全选数据时的函数

selection-change 代表选中某行数据时的函数

row-click 代表当某一行被点击时的函数

<el-table
        :data="chooseList"
        ref="multipleTable"
        @select-all="onSelectAll"
        @selection-change="handleSelectionChange"
        @row-click="onSelectOp"
        row-key="id"
      >
        <el-table-column
          type="selection"
          width="50"
          align="left"
          fixed="left"
          :reserve-selection="true"
        />
        <el-table-column type="index" width="50" align="left" label="序号" />
        <el-table-column
          label="任务名称"
          align="left"
          key="taskName"
          prop="taskName"
          min-width="120px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.taskName ? scope.row.taskName : "暂无" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="任务时长(天)"
          align="left"
          key="duration"
          prop="duration"
          min-width="120px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.duration ? scope.row.duration : "暂无" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="开始年"
          align="left"
          key="beginYear"
          prop="beginYear"
          min-width="120px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{
              scope.row.beginYear ? scope.row.beginYear : "暂无"
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="结束年"
          align="left"
          key="endYear"
          prop="endYear"
          min-width="120px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.endYear ? scope.row.endYear : "暂无" }}</span>
          </template>
        </el-table-column>
      </el-table>

2. 所需要的数据结构,接口正常返回的list结构

chooseList: [
        {
            "createBy": null,
            "createTime": "2023-11-16 10:50:26",
            "updateBy": null,
            "updateTime": null,
            "remark": null,
            "id": 165,
            "taskName": "飞行任务39",
            "orderName": "测试",
            "duration": 19,
            "beginYear": "2019",
            "endYear": "2023",
        },
        {
            "createBy": null,
            "createTime": "2023-11-15 17:34:31",
            "updateBy": null,
            "updateTime": null,
            "remark": null,
            "id": 164,
            "taskName": "测试任务-想定演示",
            "orderName": "du111506",
            "duration": 10,
            "beginYear": "2019",
            "endYear": "2023",
        }
],
selectdSingleItem: []

3. 对第一步中绑定的函数进行逻辑编写

// select-all绑定的函数
onSelectAll() {
   // 当全部选中时通过refs找到对应组件,清空所有选中的数据
   // 因为此刻是单选需求,全部选中的话是多选,因此当全部选中时要清空勾选项
   this.$refs.multipleTable.clearSelection();
},
// row-click绑定的函数
onSelectOp(row) {
   // 点击某一行时选中
   // 因为是单选,当点击某一行时应该将之前点击行的选中取消 因此要清空所有选中
   this.$refs.multipleTable.clearSelection();
   // 当点击某一行时传值的row,判断点击哪行,将该行选中
   this.$refs.multipleTable.toggleRowSelection(row, true);
},
// selection-change绑定的函数
handleSelectionChange(rows) {
   // 当勾选的行数大于1个时  rows代表所勾选的数组
   if (rows.length > 1) {
     const newRows = rows.filter((it, index) => {
       if (index == rows.length - 1) {
         this.$refs.multipleTable.toggleRowSelection(it, true);
         return true;
       } else {
         this.$refs.multipleTable.toggleRowSelection(it, false);
         return false;
       }
     });
     // 赋值给全局变量保存
     this.selectdSingleItem = newRows;
   } else {
   // 当勾选行数小于一个时,将选中的值赋值给全局变量保存
     this.selectdSingleItem = rows;
   }
},

### 如何阻止或禁用 Element UI `el-table` 的某些功能 为了阻止或禁用 `el-table` 中的特定功能,可以采用种方式来控制组件的行为。以下是几种常见的场景及其解决方案: #### 单选造 当希望将默认支持的 `el-table` 造为仅能单选时,可以在点击事件中通过编程的方式取消其他行的择状态。 ```javascript // JavaScript 部分逻辑处理 handleSelectionChange(selection, row) { if (selection.length > 1) { this.$refs.multipleTable.clearSelection(); // 清除所有择 this.$refs.multipleTable.toggleRowSelection(row); // 只保留当前行被中 } } ``` 此代码片段展示了如何监听择变化并确保每次只有一项被中的机制[^1]。 #### 控制某行不可 对于需要动态决定哪些行可的情况,则可通过设置 `<el-table-column>` 组件内的属性 `selectable` 来指定每一行是否允许被勾。 ```html <el-table-column type="selection" width="55" :selectable="(row, index) => !row.submitted"> </el-table-column> ``` 上述 HTML 片段定义了一个条件函数用于判断每条记录能否参与择操作;如果返回值为 `false` 则该行对应的复框会被自动置灰且无法响应用户的交互行为[^3]。 #### 使用 toggleRowSelection 方法 针对具体的数据项执行择/取消动作时,应该调用表格实例上的 `toggleRowSelection()` 函数而不是直接修 DOM 结构或其他间接手段。这有助于保持数据同步以及触发必要的视图更新过程。 ```javascript this.$refs.tableRef.toggleRowSelection(currentRow); ``` 这里假设有一个名为 `tableRef` 的引用指向目标表格,并且 `currentRow` 表示想要变其定状态的那一行对象[^2]。 综上所述,这些方法能够有效地帮助开发者灵活定制 `el-table` 的交互特性,满足不同应用场景下的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值