You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

VueTreeList.vue 3.1 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <div class="treeNode">
  3. <span @click="addNode">添加节点</span>
  4. <vue-tree-list
  5. :model="data"
  6. default-tree-node-name="new file"
  7. default-leaf-node-name="new folder"
  8. :default-expanded="false"
  9. @click="onClick"
  10. @change-name="onChangeName"
  11. @delete-node="onDel"
  12. @add-node="onAddNode"
  13. />
  14. </div>
  15. </template>
  16. <script>
  17. import {VueTreeList, Tree, TreeNode} from 'vue-tree-list';
  18. export default {
  19. components: {
  20. VueTreeList
  21. },
  22. props: {
  23. treeData: {
  24. type: Array,
  25. default: () => []
  26. },
  27. fileInfoParams: {
  28. type: Object,
  29. default: () => {}
  30. }
  31. },
  32. data() {
  33. return {
  34. newTree: {},
  35. fileParams: {}, // 文件相关的信息
  36. data: new Tree([
  37. {
  38. name: 'Node 1',
  39. id: 1,
  40. pid: 0,
  41. children: [
  42. {
  43. name: 'Node 1-2',
  44. id: 2,
  45. isLeaf: true,
  46. pid: 0
  47. }
  48. ]
  49. },
  50. {
  51. name: 'Node 2',
  52. id: 3,
  53. pid: 0,
  54. },
  55. {
  56. name: 'Node 3',
  57. id: 4,
  58. pid: 0
  59. }
  60. ])
  61. };
  62. },
  63. watch: {
  64. treeData(val) {
  65. this.data = new Tree(val);
  66. },
  67. fileInfoParams(val) {
  68. this.fileParams = val;
  69. }
  70. },
  71. mounted() {
  72. console.log('treeData', this.data);
  73. },
  74. methods: {
  75. onDel(node) {
  76. const paramsInfo = [];
  77. if (!node.children?.length > 0) { // 文件还是文件夹
  78. paramsInfo[0] = {...node};
  79. } else { // 文件夹
  80. node.children.filter((item) => item.sha).map((items) => { // 过滤是新增的元素进行删除
  81. paramsInfo.push({
  82. filePath: items.path,
  83. content: '',
  84. name: items.name,
  85. operation: 'delete'
  86. });
  87. });
  88. }
  89. this.$emit('deleteNode', paramsInfo);
  90. node.remove();
  91. },
  92. onChangeName(params) {
  93. console.log(params);
  94. },
  95. onAddNode(params) {
  96. console.log(params, '节点还是树');
  97. },
  98. // 点击每个文件/夹事件
  99. onClick(params) {
  100. if (!params.isLeaf) return;
  101. if (this.fileParams?.sha === params.sha) return;
  102. this.$emit('handleChangFile', params);
  103. },
  104. addNode() {
  105. console.log('我是子节点');
  106. const node = new TreeNode({name: 'new node', isLeaf: false});
  107. if (!this.data.children) this.data.children = [];
  108. this.data.addChildren(node);
  109. },
  110. }
  111. };
  112. </script>
  113. <style lang="less" rel="stylesheet/less">
  114. .vtl {
  115. .vtl-drag-disabled {
  116. background-color: #d0cfcf;
  117. &:hover {
  118. background-color: #d0cfcf;
  119. }
  120. }
  121. .vtl-disabled {
  122. background-color: #d0cfcf;
  123. }
  124. }
  125. </style>
  126. <style lang="less" rel="stylesheet/less" scoped>
  127. .icon {
  128. &:hover {
  129. cursor: pointer;
  130. }
  131. }
  132. .muted {
  133. color: gray;
  134. font-size: 80%;
  135. }
  136. </style>
  137. <style scoped>
  138. .treeNode{
  139. padding:20px 20px 0;
  140. overflow-y: auto;
  141. overflow-x: hidden;
  142. text-overflow: ellipsis;
  143. white-space: nowrap;
  144. }
  145. </style>