Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import {Injectable, OnInit} from "@angular/core";
@Injectable()
export class ToTree implements OnInit {
constructor() {
// 定义发射事件
}
ngOnInit() {
}
exists(list, parentId,myId){
for(let i=0; i<list.length; i++){
if(list[i][myId] == parentId){
return true;
}
}
return false;
}
listToTree(myId,pId,list){
const nodes = [];
for(let i=0; i<list.length; i++){
list[i].checked = false;
const row = list[i];
if ( !this.exists(list, row[pId],myId) ){
nodes.push(row);
}
}
const toDo = [];
for(let i=0; i<nodes.length; i++){
toDo.push(nodes[i]);
}
while(toDo.length){
const node = toDo.shift(); // the parent node
for(let i=0; i<list.length; i++){
const row = list[i];
if (row[pId] == node[myId]){
if (node.children){
node.children.push(row);
} else {
node.children = [row];
}
toDo.push(row);
}
}
}
return nodes;
}
}