custom-node-class.vue 1.9 KB

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