Demo6.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <script>
  2. import "he-tree-vue/dist/he-tree-vue.css";
  3. import { Tree, Fold, Check, Draggable } from "he-tree-vue";
  4. import * as hp from "helper-js";
  5. export default {
  6. extends: Tree,
  7. mixins: [Fold, Check, Draggable],
  8. props: {
  9. triggerClass: { default: "drag-trigger" },
  10. // 让树默认不可拖拽. prevent drag by default.
  11. draggable: { type: [Boolean, Function], default: false },
  12. droppable: { type: [Boolean, Function], default: false },
  13. },
  14. data() {
  15. return {
  16. treeClass: "my-tree-view1",
  17. };
  18. },
  19. computed: {
  20. total() {
  21. let i = 0;
  22. this.walkTreeData((node) => {
  23. i++;
  24. });
  25. return i;
  26. },
  27. },
  28. methods: {
  29. overrideSlotDefault({ node, index, path, tree }, original) {
  30. return (
  31. <div class="node-content">
  32. <button
  33. class="mrs fold-btn"
  34. onClick={() => tree.toggleFold(node, path)}
  35. >
  36. {node.$folded ? (
  37. <i class="el-icon-caret-right"></i>
  38. ) : (
  39. <i class="el-icon-caret-bottom"></i>
  40. )}
  41. </button>
  42. {original()}
  43. <button class="mls" onClick={() => this.addChild(node, index)}>
  44. add
  45. </button>
  46. <button class="mls">edit</button>
  47. <button class="mls" onClick={() => this.removeNodeByPath(path)}>
  48. 删除
  49. </button>
  50. </div>
  51. );
  52. },
  53. blockHeader() {
  54. return (
  55. <div class="header">
  56. <div>
  57. <button onClick={this.add}>add</button>
  58. <button onClick={this.showHidden} class="mls">
  59. show hidden
  60. </button>
  61. </div>
  62. <input
  63. onKeydown={(e) => e.key === "Enter" && this.search(e)}
  64. placeholder="Search"
  65. />
  66. </div>
  67. );
  68. },
  69. blockFooter() {
  70. return (
  71. <div class="footer">
  72. <i>Nodes count:</i> {this.total}
  73. </div>
  74. );
  75. },
  76. //
  77. add() {
  78. this.treeData.push({ text: `node ${hp.randString(3).toLowerCase()}` });
  79. },
  80. addChild(node, index) {
  81. console.log(node);
  82. this.treeData[index].children.push({ text: "child" });
  83. },
  84. showHidden() {
  85. this.walkTreeData((node) => {
  86. this.$set(node, "$hidden", false);
  87. });
  88. },
  89. hideNode(node) {
  90. this.$set(node, "$hidden", true);
  91. },
  92. search(e) {
  93. const value = e.target.value || "";
  94. this.walkTreeData((node) => {
  95. this.$set(node, "$hidden", !node.text.includes(value));
  96. });
  97. },
  98. },
  99. };
  100. </script>
  101. <style>
  102. .my-tree-view1 .mls {
  103. margin-left: 5px;
  104. }
  105. .my-tree-view1 .mrs {
  106. margin-right: 5px;
  107. }
  108. .my-tree-view1 .tree-node {
  109. padding: 0;
  110. border: none;
  111. }
  112. .my-tree-view1 .node-content {
  113. display: flex;
  114. }
  115. .my-tree-view1 .node-content .fold-btn {
  116. display: flex;
  117. justify-content: center;
  118. width: 22px;
  119. border-radius: 100%;
  120. border: none;
  121. background: 0 0;
  122. }
  123. .my-tree-view1 .tree-node-back:hover {
  124. background: #f5f5f5;
  125. }
  126. .my-tree-view1 .header {
  127. display: flex;
  128. justify-content: space-between;
  129. border-bottom: 1px solid #ccc;
  130. padding-bottom: 5px;
  131. margin-bottom: 10px;
  132. }
  133. .my-tree-view1 .footer {
  134. border-top: 1px solid #ccc;
  135. margin-top: 10px;
  136. }
  137. </style>