-
Notifications
You must be signed in to change notification settings - Fork 0
/
accordion_creator.js
71 lines (58 loc) · 1.82 KB
/
accordion_creator.js
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
#Algorithm for converting any nested object in the format which is accepted by jquery-ui Accordion
$(document).ready(function(){
//sample nested object
var obj = {
"A": {
"id": "0",
" B": {
"id": "1",
"some_value" : "2"
},
" C": {
"id": "2"
},
" D": {
"id": "3"
}
}
};
//This will find the object length
function ObjLength(obj) {
return Object.keys(obj).length;
}
// creatorAccordion function
function creatorAccordion(Main){
var accordion_string = '';
$.each(Main , function(key1 , value1){
if(key1 !== 'id')
{
accordion_string += '<h1>'+ key1 +'</h1>';
if(ObjLength(value1)==1) {
accordion_string += '<div></div>';
}
else {
accordion_string += '<div><div id="accordion">';
accordion_string += creatorAccordion(value1);
accordion_string += '</div></div>';
}
}
});
return accordion_string;
}
// DriverAccordion function
function DriverAccordion(obj) {
var accordion_string = '<div class="accordion">'
accordion_string += creatorAccordion(obj);
accordion_string += '</div>';
return accordion_string;
}
//calling the function
$("#tree").html(DriverAccordion(obj));
$( "#accordion" ).accordion({
event: "click",
collapsible: true,
heightStyle: "content",
active: false,
icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" }
});
});