forked from dabeng/JSON-Loop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
153 lines (144 loc) · 4.69 KB
/
test.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE HTML>
<head>
<style type="text/css">
div {
margin: 20px 5px;
font-size: 16px;
font-weight: bold;
color: #0071BD;
cursor: pointer;
}
#note {
color: black;
cursor: default;
}
</style>
</head>
<body>
<div id="find1">find node by Id '11'</div>
<div id="find2">find nodes by conditions {'name': 'xiaoxue'}</div>
<div id="find3">find nodes by conditions {'role': /engineer/i, 'birth': {'>=': 1985, '<': 1990}}</div>
<div id="find4">find parent node of the node with id '11'</div>
<div id="find5">find sibling nodes of the node with id '11'</div>
<div id="find6">find ancestor nodes of the node with id '12'</div>
<div id="note">Note: console of the browser will show you test result</div>
<script type="text/javascript" src="JSONLoop.js"></script>
<script tyoe="text/javascript">
var obj = {
'id': '1', 'name': 'renyang', 'birth': 1985, 'role': 'manager',
'member': [
{
'id': '2', 'name': 'huangfan', 'birth': 1983, 'role': 'manager',
'member': [
{'id': '3', 'name': 'chenxiong', 'birth': 1984, 'role': 'engineer'}
]
},
{
'id': '4', 'name': 'yuguang', 'birth': 1981, 'role': 'engineer manager',
'member': [
{'id': '5', 'name': 'chenjian', 'birth': 1985, 'role': 'engineer'}
]
},
{
'id': '6', 'name': 'deshi', 'birth': 1980, 'role': 'engineer manager',
'member': [
{'id': '7', 'name': 'haibo', 'birth': 1983, 'role': 'engineer'},
{'id': '8', 'name': 'weitao', 'birth': 1987, 'role': 'engineer'},
{'id': '9', 'name': 'liuzheng', 'birth': 1986, 'role': 'engineer'},
{'id': '10', 'name': 'xiaoxue', 'birth': 1988, 'role': 'engineer'},
{'id': '11', 'name': 'xuebin', 'birth': 1982, 'role': 'engineer',
'member': [
{'id': '12', 'name': 'sam', 'birth': 1980, 'role': 'engineer'},
{'id': '13', 'name': 'loklaan', 'birth': 1990, 'role': 'engineer'}
]
}
]
}
]
};
var jsonloop = new JSONLoop(obj, 'id', 'member');
document.getElementById('find1').addEventListener('click', function(){
jsonloop.findNodeById(obj, '11', function(err, node) {
if (err) {
console.log(err);
} else {
console.clear();
console.dir(node);
}
});
}, false);
document.getElementById('find2').addEventListener('click', function(){
jsonloop.findNodes(obj, {'name': 'xiaoxue'}, function(err, nodes) {
console.clear();
nodes.forEach(function(node) {
console.dir(node);
});
});
}, false);
document.getElementById('find3').addEventListener('click', function(){
jsonloop.findNodes(obj, {'role': /engineer/i, 'birth': {'>=': 1985, '<': 1990}},
function(err, nodes) {
console.clear();
nodes.forEach(function(node) {
console.dir(node);
});
});
}, false);
document.getElementById('find4').addEventListener('click', function(){
jsonloop.findNodeById(obj, '11', function(err, node) {
if (err) {
console.log(err);
} else {
console.clear();
jsonloop.findParent(obj, node, function(err, node) {
console.clear();
if (err) {
console.log(err);
} else {
console.dir(node);
}
});
}
});
}, false);
document.getElementById('find5').addEventListener('click', function(){
jsonloop.findNodeById(obj, '11', function(err, node) {
if (err) {
console.log(err);
} else {
console.clear();
jsonloop.findSiblings(obj, node, function(err, siblings) {
console.clear();
if (err) {
console.log(err);
} else {
siblings.forEach(function(item) {
console.dir(item);
});
}
});
}
});
}, false);
document.getElementById('find6').addEventListener('click', function(){
jsonloop.findNodeById(obj, '12', function(err, node) {
console.clear();
if (err) {
console.log(err);
} else {
jsonloop.findAncestors(obj, node, function(err, ancestors) {
console.clear();
if (err) {
console.log(err);
} else {
ancestors.forEach(function(item) {
console.dir(item);
});
}
});
}
});
}, false);
</script>
</body>
</html>