Reputation: 3038
Hi I'd like to know if its possible to populate my jstree using a loop instead of hard-coding each node. Below is my code:
api.call("Get", {
typeName: "Device"
}, function (result) {
$('#jstree').jstree({
'plugins': ["checkbox", "sort"],
'core': {
'data':
[
{ id: result[0].id, text: result[0].name },
{ id: result[1].id, text: result[1].name }
]
}
});
});
I make an api call then populate my tree with the returned results. How can I format my JSON using a loop?
This is the main part I need to change:
[
{ id: result[0].id, text: result[0].name },
{ id: result[1].id, text: result[1].name }
]
This is what I've tried:
[
function () {
for(var i = 0; i < result.length; i++){
{ id: result[i].id, text: result[i].name }
}
}
]
Thank you.
Upvotes: 1
Views: 849
Reputation: 9368
Sure.
api.call("Get", {
typeName: "Device"
}, function (result) {
$('#jstree').jstree({
'plugins': ["checkbox", "sort"],
'core': {
'data': (function () {
var results = [];
for(var i = 0; i < result.length; i++){
results.push({ id: result[i].id, text: result[i].name });
}
return results;
})()
}
});
});
Upvotes: 2