el-table多选改造 同时选中同一个id的数据(学习笔记)

这篇博客介绍了如何在Vue.js项目中使用ElementUI的el-table组件,改造多选功能,确保当选择具有相同ID的数据时,所有相关行将同时被选中。通过深入理解listData.js文件的处理逻辑,博主分享了关键代码片段和实现步骤。
摘要由CSDN通过智能技术生成
<template>
  <div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @row-click="rowClick"
      border
      :reserve-selection="true"
      @selection-change="handleSelectionChange"
    >
      <el-table-column width="55">
        <!-- slot-scope="scope" 这属性必须要 不然全选按钮有问题 -->
        <template slot="header" slot-scope="scope">
          <!-- 这一行没有意义,为了格式校验通过 -->
          <template v-if="false">{
  { scope }}</template>
          <el-checkbox v-model="isAll" @change="allChange"></el-checkbox>
        </template>
        <template slot-scope="scope">
          <span @click="stopEvent">
            <el-checkbox
              :key="scope.row.id"
              v-model="scope.row.flag"
              @change="choose(scope.row)"
            />
          </span>
        </template>
      </el-table-column>
      <el-table-column label=&#
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现el-table多选默认全部选中,你可以使用以下步骤: 1. 在el-table组件上设置`v-model`绑定一个布尔,用于控制是否选中全部数据。 ```html <el-table v-model="selectAll" :data="tableData" :row-key="row => row.id" :show-header="false"> <!-- 表格列定义 --> </el-table> ``` 2. 在Vue实例中定义`selectAll`变量,并将其初始化为`true`。 ```javascript data() { return { selectAll: true, tableData: [...], // 表格数据 }; }, ``` 3. 在el-table的模板中,添加一个全选的表头列,并绑定`selectAll`的。 ```html <template slot-scope="scope"> <el-table-column type="selection" width="55"> <template slot-scope="scope"> <el-checkbox v-model="selectAll" @change="handleSelectAll"></el-checkbox> </template> </el-table-column> <!-- 其他列定义 --> </template> ``` 4. 在Vue实例中定义`handleSelectAll`方法,用于处理全选状态的变化。当全选状态改变时,更新每一行数据选中状态。 ```javascript methods: { handleSelectAll(value) { this.tableData.forEach(row => { row.selected = value; }); }, }, ``` 5. 最后,在el-table的列定义中,设置每一行的选中状态为数据对象中的一个属性(例如`selected`),并将该属性与el-checkbox的v-model绑定。 ```html <el-table-column type="selection" width="55"> <template slot-scope="scope"> <el-checkbox v-model="scope.row.selected"></el-checkbox> </template> </el-table-column> ``` 这样,当`selectAll`变量为`true`时,全部数据将被默认选中。同时,你可以通过操作全选的复选框来控制所有行的选中状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值