demo6.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <m-tree
  3. :data="list"
  4. node-key="id"
  5. show-checkbox
  6. default-expand-all
  7. :render-content="renderContent">
  8. </m-tree>
  9. </template>
  10. <script setup>
  11. import { ref } from 'vue-demi';
  12. let id = 1000;
  13. const list = ref([
  14. {
  15. id: 1,
  16. label: '一级 1',
  17. children: [
  18. {
  19. id: 4,
  20. label: '二级 1-1',
  21. children: [
  22. {
  23. id: 10,
  24. label: '三级 1-1-1'
  25. },
  26. {
  27. id: 11,
  28. label: '三级 1-1-2'
  29. }
  30. ]
  31. },
  32. {
  33. id: 5,
  34. label: '二级 1-2'
  35. }
  36. ]
  37. },
  38. {
  39. id: 2,
  40. label: '一级 2',
  41. children: [
  42. {
  43. id: 6,
  44. label: '二级 2-1',
  45. children: [
  46. {
  47. id: 12,
  48. label: '三级 2-1-1'
  49. }
  50. ]
  51. },
  52. {
  53. id: 7,
  54. label: '二级 2-2'
  55. }
  56. ]
  57. },
  58. {
  59. id: 3,
  60. label: '一级 3',
  61. children: [
  62. {
  63. id: 8,
  64. label: '二级 3-1'
  65. },
  66. {
  67. id: 9,
  68. label: '二级 3-2'
  69. }
  70. ]
  71. }
  72. ])
  73. const renderContent = (h, { data, parentData }) => {
  74. return h(
  75. 'div',
  76. {
  77. class: 'custom-tree__item'
  78. },
  79. [
  80. h(
  81. 'span',
  82. {
  83. class: 'custom-tree__label'
  84. },
  85. data.label
  86. ),
  87. h(
  88. 'div',
  89. {
  90. style: {
  91. color: '#409eff',
  92. fontSize: '14px'
  93. }
  94. },
  95. [
  96. h(
  97. 'span',
  98. {
  99. style: {
  100. margin: '0 10px'
  101. },
  102. onClick: (e) => append(data,e)
  103. },
  104. '追加'
  105. ),
  106. h(
  107. 'span',
  108. {
  109. onClick: (e) => remove(data,parentData,e)
  110. },
  111. '删除'
  112. )
  113. ]
  114. )
  115. ]
  116. )
  117. }
  118. const append = (data,e) => {
  119. e.stopPropagation()
  120. let children = data.children || []
  121. children.push({
  122. id: id++,
  123. label: 'children-node'
  124. })
  125. list.value = [...list.value]
  126. }
  127. const remove = (data,parentData,e) => {
  128. e.stopPropagation()
  129. const index = parentData.indexOf(data)
  130. if (index > -1) {
  131. parentData.splice(index, 1)
  132. }
  133. }
  134. </script>
  135. <style>
  136. .custom-tree__item{
  137. display: flex;
  138. flex: 1;
  139. justify-content: space-between;
  140. }
  141. </style>