Vue使用Element的表格Table显示树形数据,多选框全选无法选中全部节点

使用Element的组件Table表格,当使用树形数据再配合上多选框,如下:

会出现一种问题,点击左上方全选,只能够选中一级树节点,子节点无法被选中,如图所示:

想要实现点击全选就选中所有的表格,要另想办法,方法如下:

1、首先给table设置一个ref

2、绑定一个@select-all方法;

3、定义一个变量,来识别全选框是否被选中,默认为未被选中;

复制代码
checkedKeys: false,

4、@select-all绑定的方法如下,Element的Table表格中,select-all表示当用户手动勾选全选 Checkbox 时触发的事件 ,每次点击,checkedKeys就取反,对表格数据进行foreach循环,使多选框选中/取消选中的关键代码为:

复制代码
 this.$refs.multipleTable.toggleRowSelection(row, flag);

flag=true多选框选中,flag=false取消选中

该方法不会影响@selection-change绑定的方法,若状态为全选,可以拿到全选的数据。

代码截图如下:

全部代码如下:

复制代码
<template>
  <div>
    <h1>树型数据+表格</h1>
    <el-table :data="tableData" style="width:80%;margin: 100px;" row-key="id" border default-expand-all
      @select-all="selectAll" ref="multipleTable" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="date" label="日期" sortable width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" sortable width="180">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="380">
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  nama: "Tree",
  data() {
    return {
      checkedKeys: false,
      tableData: [
        {
          id: 1,
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
          children: [],
        },
        {
          id: 2,
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1517 弄",
        },
        {
          id: 3,
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1519 弄",
          children: [
            {
              id: 31,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
            },
            {
              id: 3531,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
            },
            {
              id: 8931,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
            },
            {
              id: 32,
              date: "2016-05-01",
              name: "王小虎",
              address: "上海市普陀区金沙江路 1519 弄",
              children: [
                {
                  id: 61,
                  date: "2016-05-01",
                  name: "王小虎",
                  address: "上海市普陀区金沙江路 1519 弄",
                },
                {
                  id: 42,
                  date: "2016-05-01",
                  name: "王小虎",
                  address: "上海市普陀区金沙江路 1519 弄",
                  children: [
                    {
                      id: 321,
                      date: "2016-05-01",
                      name: "王小虎33333",
                      address: "上海市普陀区金沙江路 1519 弄",
                    },
                  ],
                },
              ],
            },
          ],
        },
        {
          id: 4,
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1516 弄",
        },
      ],
    };
  },
  methods: {
    selectAll() {
      this.checkedKeys = !this.checkedKeys;
      this.splite(this.tableData, this.checkedKeys);
    },
    /**
     * 处理数据
     */
    splite(data, flag) {
      data.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row, flag);
        if (row.children != undefined) {
          this.splite(row.children);
        }
      });
    },
    handleSelectionChange(val){
        console.log(val);
        
    }
  }
};
</script>

上述方法只能用全选,选父级的话子级是不会选中的

下面这个方法,是选择父级子级可以选中,但是全选不能用

复制代码
<template>
  <div>
    <el-table
      v-if="deptList.length > 0"
      v-loading="loading"
      :data="deptList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      @select-all="selectAll"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" :selectable="row => !row.disabled">
        <template slot-scope="scope">
          <el-checkbox v-model="scope.row.selected" @change="onRowSelectChange(scope.row)"></el-checkbox>
        </template>
      </el-table-column>
      <!-- 其他列定义 -->
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      deptList: [],
      loading: false,
      isExpandAll: false,
      checkedKeys: false
    };
  },
  methods: {
    selectAll() {
      this.checkedKeys = !this.checkedKeys;
      this.splite(this.deptList, this.checkedKeys);
    },
    splite(data, flag) {
      data.forEach((row) => {
        this.$refs.multipleTable.toggleRowSelection(row, flag);
        if (row.children && row.children.length) {
          this.splite(row.children, flag);
        }
      });
    },
    onRowSelectChange(row) {
      if (row.children && row.children.length > 0) {
        this.traverse(row.children, row.selected);
      }
    },
    traverse(data, checked) {
      data.forEach((row) => {
        this.$set(row, 'selected', checked);
        if (row.children && row.children.length > 0) {
          this.traverse(row.children, checked);
        }
      });
    },
    // 其他方法
  }
};
</script>
相关推荐
JieE21228 分钟前
LeetCode 226. 翻转二叉树|JS 递归超详细拆解,二叉树入门经典题
javascript·算法
JieE21244 分钟前
LeetCode 104. 二叉树的最大深度|递归思路超详细拆解
javascript·算法
爱勇宝1 小时前
鸿蒙生态的下半场:开发者不只要能开发,还要能赚钱
android·前端·程序员
IT_陈寒4 小时前
SpringBoot这个自动配置坑我跳了三次
前端·人工智能·后端
kyriewen5 小时前
我用 AI 一周写完了整个项目,上线第一天就崩了——这是我踩过最贵的 5 个坑
前端·javascript·ai编程
Larcher5 小时前
AI Loop:让AI像人一样自主完成任务的核心机制
javascript·人工智能·设计模式
默_笙5 小时前
🃏 JS 只有 8 种数据类型,但我花了 2 天才搞懂 null 和 undefined 的区别
javascript
牧艺5 小时前
从零到协同:构建类飞书在线文档系统的五个技术重难点
前端·人工智能
jump_jump6 小时前
流式 HTML:从 htmx 片段装配到浏览器原生增量渲染
javascript·性能优化·前端工程化
红尘散仙6 小时前
想写一个像样的终端 App?试试把 React 的开发体验搬进 Rust TUI
前端·rust