el-table树状表格多选 使用递归实现选中父节点同时勾选其所有子节点(包含点击某一行进行勾选)

el-table表格进行多选时仅能选中当前一行,对树状结构的多选来说不符合需求

下面要实现的是选中父节点,同时选中其下所有子节点:

表格中使用select方法(当用户手动勾选数据行的 Checkbox 时触发的事件)用于解决同时勾选所有子节点,row-click方法实现点击某一行进行勾选

<template>
  <div class="treeTable"
    <el-table ref="tree" :data="treeData" default-expand-all row-key="id"
      @row-click="rowClick"
      @select="handleSelection"    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="目录名称" prop="title" />
    </el-table>
  </div>
</template>

数据如下:

data() {
  return {
    treeData: [
      {
        id: '1765188101372870657', title: '目录1',
        children: [
          {
            id: '1765188210793873409', title: '目录1-1',
            children: [
              {
                id: '1765188330184736769', title: '目录1-1-1',
                children: null,
              },
              {
                id: '1765188397025165313', title: '目录1-1-2',
                children: null,
              },
            ],
          },
        ],
      },
      {
        id: '1765188457641246722', title: '目录2',
        children: [
          {
            id: '1765188542143889410', title: '目录2-1',
            children: null,
          },
          {
            id: '1765188589640187905', title: '目录2-2',
            children: [
              {
                id: '1765188652638633985', title: '目录2-2-1',
                children: null,
              },
            ],
          },
        ],
      },
    ],
  }
}

 运用选中递归方法checkRow实现勾选所有子节点:

methods: {
  //选中递归
  checkRow(data, bool) {
    data.forEach((item) => {
      this.$refs.tree.toggleRowSelection(item, bool);
      if (item.children) {
        this.checkRow(item.children, bool);
      }
    });
  },
  //选中菜单
  handleSelection(select, row) {
    if (!row.children) {
      return false;
    }
    if (this.$refs.tree.selection.includes(row)) {
      this.checkRow(row.children, true);  // 调用选中递归方法
    } else {
      this.checkRow(row.children, false);
    }
  },
  //点击表格回调
  rowClick(row) {
    if (!row.children) {
      if (this.$refs.tree.selection.includes(row)) {
        this.$refs.tree.toggleRowSelection(row, false);
      } else {
        this.$refs.tree.toggleRowSelection(row, true);
      }
      return;
    }
    if (this.$refs.tree.selection.includes(row)) {
      this.$refs.tree.toggleRowSelection(row, false);
      this.checkRow(row.children, false);  // 调用选中递归方法
    } else {
      this.$refs.tree.toggleRowSelection(row, true);
      this.checkRow(row.children, true);
    }
  },
}

以上是实现选中父节点同时选中其所有子节点的方法。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面给出一个简单的 Java 实现树形结构统计的 demo。 假设我们有一个树形结构的数据,每个节点包含一个值和若干个子节点,如下所示: ```java public class TreeNode { private int value; private List<TreeNode> children; public TreeNode(int value) { this.value = value; this.children = new ArrayList<>(); } public int getValue() { return value; } public List<TreeNode> getChildren() { return children; } public void addChild(TreeNode child) { children.add(child); } } ``` 现在我们要统计每个节点的值,满足父亲节点等于孩子节点之和加前父节点的条件。我们可以使用递归的方式实现这个功能,具体代码如下: ```java public class TreeSum { public static void main(String[] args) { // 构造树形结构数据 TreeNode root = new TreeNode(1); TreeNode node1 = new TreeNode(2); TreeNode node2 = new TreeNode(3); TreeNode node3 = new TreeNode(4); TreeNode node4 = new TreeNode(5); root.addChild(node1); root.addChild(node2); node1.addChild(node3); node1.addChild(node4); // 统计每个节点的值 calculateSum(root); // 打印每个节点的值 printTree(root); } // 统计每个节点的值 public static void calculateSum(TreeNode node) { if (node == null) { return; } // 递归处理子节点 for (TreeNode child : node.getChildren()) { calculateSum(child); } // 计算当前节点的值 int sum = node.getValue(); for (TreeNode child : node.getChildren()) { sum += child.getValue(); } if (node.getChildren().size() > 0) { sum += node.getChildren().get(0).getValue(); } node.setValue(sum); } // 打印每个节点的值 public static void printTree(TreeNode node) { if (node == null) { return; } System.out.println(node.getValue()); for (TreeNode child : node.getChildren()) { printTree(child); } } } ``` 在这个代码中,我们先构造了一个简单的树形结构数据,然后调用 `calculateSum` 方法来统计每个节点的值。在 `calculateSum` 方法中,我们先递归处理子节点,然后计算当前节点的值,并将其存储在 `value` 属性中。最后,我们调用 `printTree` 方法来打印每个节点的值。 运行这段代码,我们可以得到以下输出: ``` 15 11 9 5 6 ``` 其中,15 是根节点的值,11 是第一个子节点的值,9 是第二个子节点的值,5 是第一个子节点的第一个子节点的值,6 是第一个子节点的第二个子节点的值。可以看到,每个节点的值满足父亲节点等于孩子节点之和加前父节点的条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值