This repository was archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
59 lines (46 loc) · 1.22 KB
/
tree.js
File metadata and controls
59 lines (46 loc) · 1.22 KB
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
54
55
56
57
58
59
class Node {
constructor(title, id){
this.title = title
this.id = id
}
getTitle(){
return this.title
}
}
/* ===== Sample Tree =====
A-->B
C-->D
E-->F-->M
G-->H
==========================*/
A = new Node('A', 1)
B = new Node('B', 2)
C = new Node('C', 3)
D = new Node('D', 4)
E = new Node('E', 5)
F = new Node('F', 6)
M = new Node('M', 9)
G = new Node('G', 7)
H = new Node('H', 8)
categories = [A,[B,C,[D],E,[F,[M]]],G,[H]]
function makeList(array) {
// Create the list element:
var list = document.createElement('ul');
for (var i = 0; i < array.length; i++) {
// Create the list item:
var item = document.createElement('li');
if(Array.isArray(array[i]))
list.appendChild(makeList(array[i]) );
else
{
// Set `title` <li> ##### </li>
item.appendChild(document.createTextNode(array[i].getTitle()));
// Add it to the list:
list.appendChild(item );
item.addEventListener('click',()=>{console.log('>> Clicked!');});
}
}
// Finally, return the constructed list:
return list;
}
document.getElementById('category').appendChild(makeList(categories));