[JS/Mootools] Children Cycle
Alleen het script dat ik daarvoor heb gemaakt werkt niet, en ik hoop dat een van jullie misschien de fout kan vinden...
Hier is de JS:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function FetchIDs(el){
this.ret = new Array;
if(el.getChildren('li').length > 0){
el.getChildren('li').each(function(item){
if(item.getChildren('ul').length > 0){
var index = new Array(item.get('id'));
var na = index.concat(FetchIDs(item));
this.ret.push(na);
}else{
this.ret.push(item.get('id'));
}
},this);
}
return this.ret;
}
this.ret = new Array;
if(el.getChildren('li').length > 0){
el.getChildren('li').each(function(item){
if(item.getChildren('ul').length > 0){
var index = new Array(item.get('id'));
var na = index.concat(FetchIDs(item));
this.ret.push(na);
}else{
this.ret.push(item.get('id'));
}
},this);
}
return this.ret;
}
Voorbeeld heb ik gepost in PasteBin:
http://phphulp.pastebin.com/m7afec883
Gewijzigd op 01/01/1970 01:00:00 door Jeffrey H
Code (php)
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
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="www.serials.ws" />
<title>Untitled 2</title>
<script src="mootools-1.2.1-core-nc.js"></script>
<script>
function FetchIDs(el){
this.ret = new Array;
if(el.getChildren('li').length > 0){
alert('test_successfull');
this.ret.push('var');
el.getChildren('li').each(function(item){
if(item.getChildren('ul').length > 0){
var index = new Array(item.get('id'));
var na = index.concat(FetchIDs(item));
this.ret.push(na);
}else{
this.ret.push(item.get('id'));
}
},this);
}
return this.ret;
}
function test(){
document.getElementById('test').innerHTML = FetchIDs(document.getElementById('testing'));
//alert(FetchIDs(document.getElementById('menu')));
}
</script>
</head>
<body onload="test();">
<div id="menu">
<ul id="testing">
<li id="no1">List
<ul>
<li id="no1-1">List</li>
<li id="no1-2">List</li>
<li id="no1-3">List</li>
<li id="no1-4">List</li>
<li id="no1-5">List</li>
</ul>
</li>
<li id="no2">List
<ul>
<li id="no2-1">List</li>
<li id="no2-2">List</li>
<li id="no2-3">List</li>
<li id="no2-4">List</li>
<li id="no2-5">List</li>
</ul>
</li>
<li id="no3">List</li>
<li id="no4">List</li>
<li id="no5">List</li>
<li id="no6">List</li>
</ul>
</div>
<div id="test"></div>
</body>
</html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="www.serials.ws" />
<title>Untitled 2</title>
<script src="mootools-1.2.1-core-nc.js"></script>
<script>
function FetchIDs(el){
this.ret = new Array;
if(el.getChildren('li').length > 0){
alert('test_successfull');
this.ret.push('var');
el.getChildren('li').each(function(item){
if(item.getChildren('ul').length > 0){
var index = new Array(item.get('id'));
var na = index.concat(FetchIDs(item));
this.ret.push(na);
}else{
this.ret.push(item.get('id'));
}
},this);
}
return this.ret;
}
function test(){
document.getElementById('test').innerHTML = FetchIDs(document.getElementById('testing'));
//alert(FetchIDs(document.getElementById('menu')));
}
</script>
</head>
<body onload="test();">
<div id="menu">
<ul id="testing">
<li id="no1">List
<ul>
<li id="no1-1">List</li>
<li id="no1-2">List</li>
<li id="no1-3">List</li>
<li id="no1-4">List</li>
<li id="no1-5">List</li>
</ul>
</li>
<li id="no2">List
<ul>
<li id="no2-1">List</li>
<li id="no2-2">List</li>
<li id="no2-3">List</li>
<li id="no2-4">List</li>
<li id="no2-5">List</li>
</ul>
</li>
<li id="no3">List</li>
<li id="no4">List</li>
<li id="no5">List</li>
<li id="no6">List</li>
</ul>
</div>
<div id="test"></div>
</body>
</html>
Code (php)
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
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
function getNodeIds(el){
var nodes = new Array;
if(el.hasChildNodes()){// So, first we check if the object is not empty, if the object has child nodes
var children = el.childNodes;
for(var i = 0; i < children.length; i++){
if(children[i].tagName != undefined && children[i].tagName.toLowerCase() == 'li'){// A li node
if(el.hasChildNodes()){
var index = children[i].id;
// Get First (Non-#Text) Child
var CC = children[i].firstChild;// Child to cycle over (CycleChild)
while(CC){
if(CC.tagName != undefined && CC.tagName.toLowerCase() == 'ul'){
break;
}
CC = CC.nextSibling;
}
// Cycle through that child
var nea = getNodeIds(CC);
// Clean up the output(For when all childs have empty UL's)
if(nea.length == 0){// If the thing contains an empty UL
var na = index;
}else{// The array contains elements
var na = [index].concat(nea);
}
// And add it to our array
nodes.push(na);
}else{
nodes.push(children[i].id);
}
}// It is an TextNode, and we don't want those
}
}
return nodes;
}
var nodes = new Array;
if(el.hasChildNodes()){// So, first we check if the object is not empty, if the object has child nodes
var children = el.childNodes;
for(var i = 0; i < children.length; i++){
if(children[i].tagName != undefined && children[i].tagName.toLowerCase() == 'li'){// A li node
if(el.hasChildNodes()){
var index = children[i].id;
// Get First (Non-#Text) Child
var CC = children[i].firstChild;// Child to cycle over (CycleChild)
while(CC){
if(CC.tagName != undefined && CC.tagName.toLowerCase() == 'ul'){
break;
}
CC = CC.nextSibling;
}
// Cycle through that child
var nea = getNodeIds(CC);
// Clean up the output(For when all childs have empty UL's)
if(nea.length == 0){// If the thing contains an empty UL
var na = index;
}else{// The array contains elements
var na = [index].concat(nea);
}
// And add it to our array
nodes.push(na);
}else{
nodes.push(children[i].id);
}
}// It is an TextNode, and we don't want those
}
}
return nodes;
}
Wat het doet:
Hij cycled door een UL/LI structuur en maakt er een array structuur van (Van de id's).
Bedankt voor de moeite Robert ;)