customized-node.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <template>
  2. <div class="custom-tree-container">
  3. <p>Using render-content</p>
  4. <el-tree
  5. :data="dataSource"
  6. show-checkbox
  7. node-key="id"
  8. default-expand-all
  9. :expand-on-click-node="false"
  10. :render-content="renderContent"
  11. />
  12. <p>Using scoped slot</p>
  13. <el-tree
  14. :data="dataSource"
  15. show-checkbox
  16. node-key="id"
  17. default-expand-all
  18. :expand-on-click-node="false"
  19. >
  20. <template #default="{ node, data }">
  21. <span class="custom-tree-node">
  22. <span>{{ node.label }}</span>
  23. <span>
  24. <a @click="append(data)"> Append </a>
  25. <a style="margin-left: 8px" @click="remove(node, data)"> Delete </a>
  26. </span>
  27. </span>
  28. </template>
  29. </el-tree>
  30. </div>
  31. </template>
  32. <script lang="ts" setup>
  33. import { ref } from 'vue'
  34. import type Node from 'element-plus/es/components/tree/src/model/node'
  35. interface Tree {
  36. id: number
  37. label: string
  38. children?: Tree[]
  39. }
  40. let id = 1000
  41. const append = (data: Tree) => {
  42. const newChild = { id: id++, label: 'testtest', children: [] }
  43. if (!data.children) {
  44. data.children = []
  45. }
  46. data.children.push(newChild)
  47. dataSource.value = [...dataSource.value]
  48. }
  49. const remove = (node: Node, data: Tree) => {
  50. const parent = node.parent
  51. const children: Tree[] = parent.data.children || parent.data
  52. const index = children.findIndex((d) => d.id === data.id)
  53. children.splice(index, 1)
  54. dataSource.value = [...dataSource.value]
  55. }
  56. const renderContent = (
  57. h,
  58. {
  59. node,
  60. data,
  61. store,
  62. }: {
  63. node: Node
  64. data: Tree
  65. store: Node['store']
  66. }
  67. ) => {
  68. return h(
  69. 'span',
  70. {
  71. class: 'custom-tree-node',
  72. },
  73. h('span', null, node.label),
  74. h(
  75. 'span',
  76. null,
  77. h(
  78. 'a',
  79. {
  80. onClick: () => append(data),
  81. },
  82. 'Append '
  83. ),
  84. h(
  85. 'a',
  86. {
  87. style: 'margin-left: 8px',
  88. onClick: () => remove(node, data),
  89. },
  90. 'Delete'
  91. )
  92. )
  93. )
  94. }
  95. const dataSource = ref<Tree[]>([
  96. {
  97. id: 1,
  98. label: 'Level one 1',
  99. children: [
  100. {
  101. id: 4,
  102. label: 'Level two 1-1',
  103. children: [
  104. {
  105. id: 9,
  106. label: 'Level three 1-1-1',
  107. },
  108. {
  109. id: 10,
  110. label: 'Level three 1-1-2',
  111. },
  112. ],
  113. },
  114. ],
  115. },
  116. {
  117. id: 2,
  118. label: 'Level one 2',
  119. children: [
  120. {
  121. id: 5,
  122. label: 'Level two 2-1',
  123. },
  124. {
  125. id: 6,
  126. label: 'Level two 2-2',
  127. },
  128. ],
  129. },
  130. {
  131. id: 3,
  132. label: 'Level one 3',
  133. children: [
  134. {
  135. id: 7,
  136. label: 'Level two 3-1',
  137. },
  138. {
  139. id: 8,
  140. label: 'Level two 3-2',
  141. },
  142. ],
  143. },
  144. ])
  145. </script>
  146. <style>
  147. .custom-tree-node {
  148. flex: 1;
  149. display: flex;
  150. align-items: center;
  151. justify-content: space-between;
  152. font-size: 14px;
  153. padding-right: 8px;
  154. }
  155. </style>