custom-node-class.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <div class="custom-tree-node-container">
  3. <el-tree
  4. :data="data"
  5. show-checkbox
  6. node-key="id"
  7. default-expand-all
  8. :expand-on-click-node="false"
  9. :props="{ class: customNodeClass }"
  10. />
  11. </div>
  12. </template>
  13. <script lang="ts" setup>
  14. import type Node from 'element-plus/es/components/tree/src/model/node'
  15. interface Tree {
  16. id: number
  17. label: string
  18. isPenultimate?: boolean
  19. children?: Tree[]
  20. }
  21. const customNodeClass = (data: Tree, node: Node) => {
  22. if (data.isPenultimate) {
  23. return 'is-penultimate'
  24. }
  25. return null
  26. }
  27. const data: Tree[] = [
  28. {
  29. id: 1,
  30. label: 'Level one 1',
  31. children: [
  32. {
  33. id: 4,
  34. label: 'Level two 1-1',
  35. isPenultimate: true,
  36. children: [
  37. {
  38. id: 9,
  39. label: 'Level three 1-1-1',
  40. },
  41. {
  42. id: 10,
  43. label: 'Level three 1-1-2',
  44. },
  45. ],
  46. },
  47. ],
  48. },
  49. {
  50. id: 2,
  51. label: 'Level one 2',
  52. isPenultimate: true,
  53. children: [
  54. {
  55. id: 5,
  56. label: 'Level two 2-1',
  57. },
  58. {
  59. id: 6,
  60. label: 'Level two 2-2',
  61. },
  62. ],
  63. },
  64. {
  65. id: 3,
  66. label: 'Level one 3',
  67. isPenultimate: true,
  68. children: [
  69. {
  70. id: 7,
  71. label: 'Level two 3-1',
  72. },
  73. {
  74. id: 8,
  75. label: 'Level two 3-2',
  76. },
  77. ],
  78. },
  79. ]
  80. </script>
  81. <style>
  82. .is-penultimate > .el-tree-node__content {
  83. color: #626aef;
  84. }
  85. .el-tree-node.is-expanded.is-penultimate > .el-tree-node__children {
  86. display: flex;
  87. flex-direction: row;
  88. }
  89. .is-penultimate > .el-tree-node__children > div {
  90. width: 25%;
  91. }
  92. </style>