当前位置:   article > 正文

vue3+element-plus实现Tree树控件可增删改查_vue项目 树形控件三级联动 可对每一级进行修改和删除

vue项目 树形控件三级联动 可对每一级进行修改和删除

1.在element-plus组件库中只有增删树形组件的功能,并没有编辑节点的功能;目前我们对组件做修改实现对节点可编辑;

2.template中代码如下

  1. <template>
  2. <div>
  3. <el-tree :data="data" show-checkbox node-key="id" default-expand-all :expand-on-click-node="false"
  4. :render-content="renderContent"></el-tree>
  5. </div>
  6. </template>

3.script代码

  1. <script setup>
  2. import { ref } from 'vue';
  3. let id = 1000;
  4. const data = ref([
  5. {
  6. id: 1,
  7. label: '一级 1',
  8. children: [
  9. {
  10. id: 4,
  11. label: '二级 1-1',
  12. children: [
  13. {
  14. id: 9,
  15. label: '三级 1-1-1'
  16. },
  17. {
  18. id: 10,
  19. label: '三级 1-1-2'
  20. }
  21. ]
  22. }
  23. ]
  24. },
  25. {
  26. id: 2,
  27. label: '一级 2',
  28. children: [
  29. {
  30. id: 5,
  31. label: '二级 2-1'
  32. },
  33. {
  34. id: 6,
  35. label: '二级 2-2'
  36. }
  37. ]
  38. },
  39. {
  40. id: 3,
  41. label: '一级 3',
  42. children: [
  43. {
  44. id: 7,
  45. label: '二级 3-1'
  46. },
  47. {
  48. id: 8,
  49. label: '二级 3-2'
  50. }
  51. ]
  52. }
  53. ])
  54. // 添加
  55. function append(data) {
  56. const newChild = { id: id++, label: 'testtest', children: [] };
  57. if (!data.children) {
  58. data.children = []
  59. }
  60. data.children.push(newChild);
  61. }
  62. // 删除
  63. function remove(node, data) {
  64. const parent = node.parent;
  65. const children = parent.data.children || parent.data;
  66. const index = children.findIndex(d => d.id === data.id);
  67. children.splice(index, 1);
  68. }
  69. // 编辑
  70. function edit(data) {
  71. ElMessageBox.prompt('请输入标题', '编辑', {
  72. confirmButtonText: '确定',
  73. cancelButtonText: '取消',
  74. }).then(({ value }) => {
  75. if (value != null) {
  76. data.label = value;
  77. }
  78. }).catch(() => { });
  79. }
  80. // 按钮渲染
  81. function renderContent(h, { node, data, store }) {
  82. return h(
  83. "div",
  84. {
  85. class: "custom-tree-node",
  86. style: { width: '100%', display: "flex", justifyContent: "space-between" } // 设置父元素的样式
  87. },
  88. [
  89. h(
  90. "span",
  91. {
  92. class: "node-label",
  93. style: { flex: "1" } // 设置节点名称的样式,使其占据剩余空间
  94. },
  95. node.label
  96. ),
  97. h(
  98. "div",
  99. { class: "action-links" },
  100. [
  101. h(
  102. "a",
  103. {
  104. onClick: () => append(data),
  105. style: { marginLeft: "50px", color: "#3c92fc" } // 设置添加按钮的样式,设置左边距
  106. },
  107. " 添加 "
  108. ),
  109. h(
  110. "a",
  111. {
  112. onClick: () => edit(data),
  113. style: { marginLeft: "10px", color: "#3c92fc" } // 设置编辑按钮的样式,设置左边距
  114. },
  115. " 编辑 "
  116. ),
  117. h(
  118. "a",
  119. {
  120. onClick: () => remove(node, data, store),
  121. style: { marginLeft: "10px", color: "#fd181d" } // 设置删除按钮的样式,设置左边距
  122. },
  123. " 删除 "
  124. ),
  125. ]
  126. )
  127. ]
  128. );
  129. }
  130. </script>

4.在style无代码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/903370
推荐阅读
相关标签
  

闽ICP备14008679号