Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  3] [ 3]  / answers: 1 / hits: 15758  / 10 Years ago, sun, november 9, 2014, 12:00:00

I have JSON data and that JSON data has parent child relation . I Want to create tree structure from it. i found many plugins and libraries but i can't found my requirement . I am getting this JSON data using PHP script.



Here is image that has tree structure that i want to create . i'm stuck at it.I know JSON is not as displayed in image but i only want to show you what a tree should look like .How to create tree like in image.All i want is javascript code to handle and create this type of structure of tree . Working example is must & much appreciated.



You can use JSON format as you like and tree should be collapsible.Also provide required JSON format for it.



enter



and my JSON data as follows :



     {
2:
{
5: Wrist Watch
},
5:
{
9: Men's
},
18:
{
3: Clothing
},
28:
{
1: Perfumes
},
29:
{
7: Laptop,
10: Tablets
},
30:
{
8: Mobile
},
31:
{
2: Books
},
33:
{
6: Electronics
},
34:
{
4: Home & Kitchenn
}
}

More From » jquery

 Answers
4

If you want to roll your own, the keyword in trees is recursion. It needs to support any depth of data and the code and data should both support recursion.



This means your JSON data should be a recursive structure, where each node looks the same (and looks something like this):



{
id: 1, // data id
title: title, // display title
children: [ // list of children, each with this same structure
// list of child nodes
]
}


Note: I have changed the sample data to contain more depth as 2 levels never shows up recursion problems.



e.g.:



{
id: 0,
title: root - not displayed,
children: [{
id: 1,
title: Option 1,
children: [{
id: 11,
title: Option 11,
children: [{
id: 111,
title: Option 111
}, {
id: 112,
title: Option 112
}]
}, {
id: 12,
title: Option 12
}]
}, {
id: 2,
title: Option 2,
children: [{
id: 21,
title: Option 21
}, {
id: 22,
title: Option 22
}]
}, {
id: 3,
title: Option 3,
children: [{
id: 31,
title: Option 31
}, {
id: 32,
title: Option 32
}]
}]
}


The recursive function looks like this:



function addItem(parentUL, branch) {
for (var key in branch.children) {
var item = branch.children[key];
$item = $('<li>', {
id: item + item.id
});
$item.append($('<input>', {
type: checkbox,
name: item + item.id
}));
$item.append($('<label>', {
for: item + item.id,
text: item.title
}));
parentUL.append($item);
if (item.children) {
var $ul = $('<ul>').appendTo($item);
addItem($ul, item);
}
}
}


JSFiddle: http://jsfiddle.net/0s0p3716/188/



The code recurses the structure, adding new ULs and LIs (with checkbox etc ) as it goes. The top level call just provides the initial root starting points of both the display and the data.



addItem($('#root'), data);


The end result looks like this:



enter



If you want to toggle visibility, based on the checked state, use this:



$(':checkbox').change(function () {
$(this).closest('li').children('ul').slideToggle();
});


If you also want the labels to toggle the checkboxes, use this:



$('label').click(function(){
$(this).closest('li').find(':checkbox').trigger('click');
});


Note: I have only provided the most basic of styling as that will typically be to taste. Examples in links were shown in another answer.



-- updated:




amended: possible wrong ids for items 31 & 32?




function for better selection and deselection(for parents cascading into child nodes):



$(function () {
addItem($('#root'), data);
$(':checkbox').click(function () {
$(this).find(':checkbox').trigger('click');
var matchingId = $(this).attr('id');
if ($(this).attr('checked'))
{
$('input[id*=' + matchingId +']').each(function() {
$(this).removeAttr('checked');
$(this).prop('checked', $(this).attr('checked'));
});
}
else {
$('input[id*=' + matchingId +']').each(function() {
$(this).attr('checked', 'checked');
$(this).prop('checked', $(this).attr('checked'));

});
}

});
$('label').click(function(){
$(this).closest('li').children('ul').slideToggle();

});


-- Update the fiddle with this as shown here(JsFiddle) and it will work better and also will allow you to click the text to expand without selecting at the same time - I know I find this far more useful. It will help (and this is personal preference) you to see what values and options are available without having to select the first.


[#68859] Thursday, November 6, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myrap

Total Points: 407
Total Questions: 105
Total Answers: 109

Location: Cambodia
Member since Thu, Oct 7, 2021
3 Years ago
myrap questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Wed, Jan 15, 20, 00:00, 4 Years ago
Thu, Oct 24, 19, 00:00, 5 Years ago
Thu, Oct 3, 19, 00:00, 5 Years ago
Mon, Aug 12, 19, 00:00, 5 Years ago
;