@@ -8,7 +8,7 @@ Description:
88
99<template >
1010 <div class =" container" >
11- <myTree :nodes =" data " />
11+ <myTree :nodes =" computedTree " />
1212 </div >
1313</template >
1414
@@ -23,16 +23,36 @@ export default {
2323 },
2424
2525 setup () {
26- const data = ref ( [
26+ const data = [
2727 {
28- label: " father" ,
28+ id: 1 ,
29+ label: ' Animal' ,
2930 nodes: [
3031 {
31- label: " son1" ,
32+ id: 2 ,
33+ label: ' Dog' ,
34+ },
35+ {
36+ id: 3 ,
37+ label: ' Cat' ,
38+ nodes: [
39+ {
40+ id: 4 ,
41+ label: ' Egyptian Mau Cat' ,
42+ },
43+ {
44+ id: 5 ,
45+ label: ' Japanese Bobtail Cat' ,
46+ },
47+ ],
3248 },
3349 ],
3450 },
35- ]);
51+ {
52+ id: 6 ,
53+ label: ' People' ,
54+ },
55+ ];
3656 return {
3757 data,
3858 };
@@ -42,59 +62,93 @@ export default {
4262 ... mapState ([" componentMap" , " activeComponent" ]),
4363 // Returns project tree on re-render
4464 computedTree () {
45- console .log (" buildtree" , this .buildTree ());
46- return this .buildTree ();
65+ const builtTree = [this .buildTree ()];
66+ console .log (" buildtree" , builtTree);
67+ return builtTree;
4768 },
4869 },
4970
5071 data () {
5172 return {
5273 tree: null ,
5374 treeKey: " key" ,
54- testTree: {
55- label: " father" ,
75+ testTree: [
76+ {
77+ id: 1 ,
78+ label: ' Animal' ,
5679 nodes: [
5780 {
58- label: " son1" ,
81+ id: 2 ,
82+ label: ' Dog' ,
83+ },
84+ {
85+ id: 3 ,
86+ label: ' Cat' ,
87+ nodes: [
88+ {
89+ id: 4 ,
90+ label: ' Egyptian Mau Cat' ,
91+ },
92+ {
93+ id: 5 ,
94+ label: ' Japanese Bobtail Cat' ,
95+ },
96+ ],
5997 },
6098 ],
6199 },
100+ {
101+ id: 6 ,
102+ label: ' People' ,
103+ },
104+ ],
62105 };
63106 },
64107
65108 methods: {
66109 // Called by transformToTree, formats componentMap
67110 formatComponentMap (compMap ) {
111+ // console.log('COMP MAP: ', compMap);
68112 let result = [];
69- Object .values (compMap).forEach ((compData ) => {
113+ // Id to apply to each component, in accordance with Vue3Tree syntax
114+ let id = 1 ;
115+ const values = Object .values (compMap)
116+ // console.log('FormatComponentMap: ', values);
117+ values .forEach ((compData ) => {
70118 result .push ({
71- name: compData .componentName ,
72- children: compData .children ,
119+ id: id++ ,
120+ label: compData .componentName , // previously was labeled 'name'
121+ nodes: compData .children , // previously was labeled 'children'
73122 });
74123 });
124+
125+
126+ console .log (' FORMATCOMPONENTMAP result: ' , result);
75127 return result;
76128 },
77129 // Called by buildTree, transforms componentMap
130+ // we need to change this to clean the data and transform it to something usable by vue3tree
78131 transformToTree (data ) {
79132 let result = {};
80- const nodes = {};
133+ const temp = {};
81134 const formattedData = this .formatComponentMap (data);
82135 formattedData .forEach ((component ) => {
83- if (! nodes [component .name ]) {
84- nodes [component .name ] = { name : component .name , children : [] };
85- result = nodes ;
136+ if (! temp [component .label ]) {
137+ temp [component .label ] = { label : component .label , nodes : [] };
138+ result = temp ;
86139 }
87- component .children .forEach ((child ) => {
88- if (! nodes [child]) nodes [child] = { name : child, children : [] };
89- nodes [component .name ]. children .push (nodes [child]);
140+ component .nodes .forEach ((child ) => {
141+ if (! temp [child]) temp [child] = { label : child, nodes : [] };
142+ temp [component .label ]. nodes .push (temp [child]);
90143 });
91144 });
92- // console.log(nodes)
93- // console.log(result)
145+ // console.log('transformToTree nodes:', nodes)
146+ console .log (' TRANSFORMTOTREE result ' , result)
94147 return result;
95148 },
96149 // Called by computedTree, calls transformToTree
97150 buildTree () {
151+ console .log (' COMPONENT MAP:' , this .componentMap );
98152 let build = this .transformToTree (this .componentMap );
99153 return build[" App" ];
100154 },
0 commit comments