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>
相关推荐
bjzhang758 小时前
使用 HTML + JavaScript 实现在线知识挑战
前端·javascript·html
全栈王校长8 小时前
Vue.js 3 模板语法与JSX语法详解
vue.js
薛定谔的猫28 小时前
Cursor 系列(3):关于MCP
前端·cursor·mcp
全栈王校长8 小时前
Vue.js 3 项目构建:从 Webpack 到 Vite 的转变之路
vue.js
sheji34168 小时前
【开题答辩全过程】以 基于web的拍卖系统设计与实现为例,包含答辩的问题和答案
前端
明月_清风8 小时前
模仿 create-vite / create-vue 风格写一个现代脚手架
前端·后端
aou8 小时前
让表格式录入像 Excel 一样顺滑
前端·ai编程
前端付豪8 小时前
必知 Express和 MVC
前端·node.js·全栈
重铸码农荣光8 小时前
CSS 也能“私有化”?揭秘模块化 CSS 的防坑指南(附 Vue & React 实战)
前端·css·vue.js
南囝coding8 小时前
CSS终于能做瀑布流了!三行代码搞定,告别JavaScript布局
前端·后端·面试