<template> <div> <!-- <button @click="addNode">Add Node</button> --> <vue-tree-list class="vue-tree-list" @click="onClick" @change-name="onChangeName" @delete-node="onDel" @add-node="onAddNode" :model="dataTree" default-tree-node-name="new node" default-leaf-node-name="new leaf" v-bind:default-expanded="true" ref="treee" > <!-- <div v-slot:leafNameDisplay="slotProps"> <span> {{ slotProps.model[titleKey] }} <span v-if="slotProps.model.count" class="muted" >#{{ slotProps.model.count }}</span > </span> </div> --> <!-- <span class="icon" slot="addTreeNodeIcon">📂</span> <span class="icon" slot="addLeafNodeIcon">+</span> <span class="icon" slot="editNodeIcon">📃</span> <span class="icon" slot="delNodeIcon">✂️</span> <span class="icon" slot="leafNodeIcon">🍃</span> <span class="icon" slot="treeNodeIcon">🌲</span> --> <span class="icon1" slot="treeNodeIcon"> <div style=" width: 6px; height: 6px; border-radius: 50%; background-color: black; " ></div> </span> </vue-tree-list> <!-- <button @click="getNewTree">Get new tree</button> <pre> {{ newTree }} </pre> --> </div> </template> <script> import { VueTreeList, Tree, TreeNode } from "vue-tree-list"; export default { props: { parentName: { default: "Thing", }, titleKey: { default: "title", }, treeItems: { default() { return []; }, }, }, data() { return { // newTree: {}, dicTreePath: {}, dataTree: new Tree([ { id: "Thing", value: "Thing", }, ]), }; }, // mounted() { // data= new Tree(this.treeItems); // }, watch: { treeItems(newList) { // this.dicTreePath = {}; // this.dataTree.children = newVal; // this.dataTree.children.forEach((element,index) => { // this.dicTreePath['parent'] = 'Thing'; // }); newList.forEach((element, index) => { const nodeModel = { children: null, id: index, isLeaf: false, parent: this.parentName, pid: this.parentName, value: element.value, name: element.value, }; var node = new TreeNode(nodeModel); // todo: find the parent and add new children into it. // this.dataTree.children if (!this.dataTree.children && !this.dataTree.children[0]) this.dataTree.children[0].children = []; this.dataTree.children[0].addChildren(node); }); }, }, methods: { onDel(node) { console.log(node); node.remove(); this.$emit("on-delete-node", node); }, onChangeName(params) { console.log(params); this.$emit("on-change-name", params); }, onAddNode(params) { console.log(params); this.$emit("on-add-node", params); }, onClick(params) { console.log(params); this.$emit("on-click", params); }, addNode(item) { var node = new TreeNode({ item: item, isLeaf: false }); if (!this.dataTree.children) this.dataTree.children = []; this.dataTree.addChildren(node); }, updateChildren(element, childs) { var parent = this.dataTree; if (element.parent != null) parent = element.parent; const index = parent.children.findIndex( (item) => item.keyId === element.keyId ); if (index !== -1) { parent.children[index].children = childs; } }, getNewTree() { var vm = this; function _dfs(oldNode) { var newNode = {}; for (var k in oldNode) { if (k !== "children" && k !== "parent") { newNode[k] = oldNode[k]; } } if (oldNode.children && oldNode.children.length > 0) { newNode.children = []; for (var i = 0, len = oldNode.children.length; i < len; i++) { newNode.children.push(_dfs(oldNode.children[i])); } } return newNode; } vm.newTree = _dfs(vm.dataTree); }, }, }; </script> <style lang="scss"> .vtl-node-main { padding: 5px 1rem 5px 0 !important; .vtl-node-content { margin-right: 7px; } &.vtl-drag-disabled { // background-color: #d0cfcf; background-color: inherit !important; } &:hover { cursor: pointer; background-color: #f0f0f0 !important; } .vtl-caret { margin-left: 0; margin-right: -1rem; margin-top: 8px; transform: rotateY(180deg); } } .vtl-tree-margin { margin-right: 2em; margin-left: auto; } .vtl { .vtl-drag-disabled { background-color: #d0cfcf; &:hover { background-color: #d0cfcf; } } .vtl-disabled { background-color: #d0cfcf; } } .vtl { } </style> <style lang="scss" scoped> .icon { &:hover { cursor: pointer; } } .muted { color: gray; font-size: 80%; } </style>