• 使用el-table的树状结构数据的勾选列,可以勾选第一层级,但是第二级之后的都不支持勾选


    一、首先我们处理题目需求:

    (1)勾选所有子节点后自动勾选父节点

    (2)勾选父节点会自动勾选所有子节点

    html代码:

    1. <el-table
    2. :data="tableDataRight"
    3. stripe
    4. height="calc(100% - 50px)"
    5. @selection-change="handleSelectionChange"
    6. @row-dblclick="onRowDblclick"
    7. @sort-change="onSortChanged"
    8. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    9. row-key="id"
    10. default-expand-all="true"
    11. ref="table"
    12. @select="handleSelect"
    13. >

    data代码:

          tableDataRight: [],

    method代码:

    1. handleSelect(selection, row) {
    2. if (row.children) {
    3. // 如果是父节点,则同时勾选其子节点
    4. row.children.forEach((child) => {
    5. this.$refs.table.toggleRowSelection(child);
    6. });
    7. } else if (row.pid) {
    8. // 如果是子节点且有父节点,则判断是否勾选父节点
    9. const parent = this.tableDataRight.find((item) => item.id === row.pid);
    10. if (parent) {
    11. // 判断是否所有子节点都被勾选
    12. const allChildrenSelected = parent.children.every((child) =>
    13. selection.includes(child)
    14. );
    15. if (allChildrenSelected) {
    16. // 如果所有子节点都被勾选,则勾选父节点
    17. this.$refs.table.toggleRowSelection(parent);
    18. }
    19. }
    20. }
    21. },

    二、问题一:勾选完子节点和父节点后,如果我去取消勾选所有的子节点,父节点还是勾选状态

    这个很简单只需要多加个else语句就行

    1. handleSelect(selection, row) {
    2. if (row.children) {
    3. // 如果是父节点,则同时勾选其子节点
    4. row.children.forEach((child) => {
    5. this.$refs.table.toggleRowSelection(child);
    6. });
    7. } else if (row.pid) {
    8. // 如果是子节点且有父节点,则判断是否勾选父节点
    9. const parent = this.tableDataRight.find((item) => item.id === row.pid);
    10. if (parent) {
    11. // 判断是否所有子节点都被勾选
    12. const allChildrenSelected = parent.children.every((child) =>
    13. selection.includes(child)
    14. );
    15. if (allChildrenSelected) {
    16. // 如果所有子节点都被勾选,则勾选父节点
    17. this.$refs.table.toggleRowSelection(parent);
    18. } else {
    19. // 否则,取消勾选父节点
    20. this.$refs.table.toggleRowSelection(parent, false);
    21. }
    22. }
    23. }
    24. },

    三、问题二:先勾选一两个子节点,然后点击勾选父节点,这两个勾选的节点被取消勾选,其他没被勾选的子节点被勾选

    希望在勾选父节点时,不影响已勾选的子节点,并且只勾选未勾选的子节点

    1. handleSelect(selection, row) {
    2. if (row.children) {
    3. // 这个是父节点取消勾选的情况,将所有子节点全部取消勾选
    4. let allSelected = row.children.every((child) => selection.includes(child));
    5. if (allSelected) {
    6. // 如果所有子节点都被勾选,则全部取消勾选
    7. row.children.forEach((child) => {
    8. this.$refs.table.toggleRowSelection(child), false;
    9. });
    10. } else {
    11. row.children.forEach((child) => {
    12. // 如果没有被勾选就勾选
    13. if (!selection.includes(child)) {
    14. this.$refs.table.toggleRowSelection(child);
    15. }
    16. });
    17. }
    18. } else if (row.pid) {
    19. // 如果是子节点且有父节点,则判断是否勾选父节点
    20. const parent = this.tableDataRight.find((item) => item.id === row.pid);
    21. if (parent) {
    22. // 判断是否所有子节点都被勾选
    23. const allChildrenSelected = parent.children.every((child) =>
    24. selection.includes(child)
    25. );
    26. if (allChildrenSelected) {
    27. // 如果所有子节点都被勾选,则勾选父节点
    28. this.$refs.table.toggleRowSelection(parent);
    29. } else {
    30. // 否则,取消勾选父节点
    31. this.$refs.table.toggleRowSelection(parent, false);
    32. }
    33. }
    34. }
    35. },

    四、问题三:总勾选框只能勾选一级数据

    html代码(在<el-table>添加):

    @select-all="selectAll"

    method代码:

    1. selectAll(selection) {
    2. // 根据勾选状态判断是否勾选或取消勾选所有子节点
    3. this.tableDataRight.forEach((row) => {
    4. if (!row.children || row.children.length === 0) {
    5. return;
    6. }
    7. // 取消勾选所有子节点,不管之前是否有取消勾选过的子节点
    8. row.children.forEach((child) => {
    9. this.$refs.table.toggleRowSelection(child, false);
    10. });
    11. if (selection.includes(row)) {
    12. // 勾选父节点时,勾选所有子节点
    13. row.children.forEach((child) => {
    14. this.$refs.table.toggleRowSelection(child);
    15. });
    16. }
    17. });
    18. },

    总结:所有代码

    html代码:

    1. <el-table
    2. :data="tableDataRight"
    3. stripe
    4. height="calc(100% - 50px)"
    5. @selection-change="handleSelectionChange"
    6. @row-dblclick="onRowDblclick"
    7. @sort-change="onSortChanged"
    8. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    9. row-key="id"
    10. default-expand-all="true"
    11. ref="table"
    12. @select="handleSelect"
    13. @select-all="selectAll"
    14. >

    js代码:

    1. handleSelect(selection, row) {
    2. if (row.children) {
    3. // 这个是父节点取消勾选的情况,将所有子节点全部取消勾选
    4. let allSelected = row.children.every((child) =>
    5. selection.includes(child)
    6. );
    7. if (allSelected) {
    8. // 如果所有子节点都被勾选,则全部取消勾选
    9. row.children.forEach((child) => {
    10. this.$refs.table.toggleRowSelection(child), false;
    11. });
    12. } else {
    13. row.children.forEach((child) => {
    14. // 如果没有被勾选就勾选
    15. if (!selection.includes(child)) {
    16. this.$refs.table.toggleRowSelection(child);
    17. }
    18. });
    19. }
    20. } else if (row.pid) {
    21. // 如果是子节点且有父节点,则判断是否勾选父节点
    22. const parent = this.tableDataRight.find((item) => item.id === row.pid);
    23. if (parent) {
    24. // 判断是否所有子节点都被勾选
    25. const allChildrenSelected = parent.children.every((child) =>
    26. selection.includes(child)
    27. );
    28. if (allChildrenSelected) {
    29. // 如果所有子节点都被勾选,则勾选父节点
    30. this.$refs.table.toggleRowSelection(parent);
    31. } else {
    32. // 否则,取消勾选父节点
    33. this.$refs.table.toggleRowSelection(parent, false);
    34. }
    35. }
    36. }
    37. },
    38. selectAll(selection) {
    39. // 根据勾选状态判断是否勾选或取消勾选所有子节点
    40. this.tableDataRight.forEach((row) => {
    41. if (!row.children || row.children.length === 0) {
    42. return;
    43. }
    44. // 取消勾选所有子节点,不管之前是否有取消勾选过的子节点
    45. row.children.forEach((child) => {
    46. this.$refs.table.toggleRowSelection(child, false);
    47. });
    48. if (selection.includes(row)) {
    49. // 勾选父节点时,勾选所有子节点
    50. row.children.forEach((child) => {
    51. this.$refs.table.toggleRowSelection(child);
    52. });
    53. }
    54. });
    55. },

  • 相关阅读:
    flutter 实现表单的封装包含下拉框和输入框
    (四)DDD之“架构”——没有规矩,不成方圆
    java:数组缩减
    【数据结构】—堆排序以及TOP-K问题究极详解(含C语言实现)
    comfyui安装指南及animaldiff使用
    解决Maven打包Nacos时插件报错
    学习SLAM:SLAM进阶(九)以激光点云赋色为例讲述如何自定义ROS的消息格式并实现消息的订阅与发布
    Tornado 被制裁 还有哪些替代工具?
    塔望3W消费战略全案丨轻食植物基,突围侧翼战
    Vue开发历程---音乐播放器
  • 原文地址:https://blog.csdn.net/qq_42192641/article/details/133740745