I’m trying to make a <table> through a javascript function.
I’m getting a JSON element that looks like that :
header
["Nom", "Région", "Activité", 10 more...]
0 "Nom"
1 "Région"
2 "Activité"
// other fields
body
Object { entity0=[13], entity1=[13], entity2=[13], more...}
entity0
["Org2", "Org2", "Org2", 10 more...]
0 "Org2"
1 "Org2"
2 "Org2"
//Other fields
entity1
["gfhu", "rtyud", "dgud", 10 more...]
//Other entities
And I’m trying to decode it like that (I parse the JSON and give it to that function) :
function createTableEntity(tab, id){
table = '<table cellpadding="0" cellspacing="0" border="0" class="display" id="'+id+'">';
table = table + '<thead><tr>';
$(tab.header).children().each(function(){
table = table + '<td>' + this + '</td>';
});
table = table + '</tr></thead>';
table = table + '<tbody>';
$(tab.body).children().each(function(){
table = table + '<tr>';
$(this).children().each(function(){
table = table + '<td>' + $(this) + '</td>';
});
table = table + '</tr>';
});
table = table + '</tbody>';
table = table + '</table>';
//alert(table);
return table;
}
From the results I have, there are no children ($(tab.header).children().each(function(){});).
Where does it come from? How do I loop through the elements parsed from JSON?
You don’t need jquery to loop through the result of a JSON parsing as it’s a Javascript object.
If you have an array in tab.header, you simply can loop on it with
Or more classicaly, without jquery, using