Reputation: 47
$.noConflict();
jQuery(document).ready(function($){
var queuesArr = new Array();
var i;
for (i = 0; i < document.getElementById('qbeansLength').value; i++){
queuesArr[i] = document.getElementById('queueID'+i).value}
var data = [
{
label: ' Queues',
children: [
//I need to write the for loop here and make it like this {label: queuesArr[i] } but it is a variable
{ label: document.getElementById('queueID1').value },
{ label: queuesArr[2] }
]
},
{
label: 'node2',
children: [
{ label: 'child3' }
]
}
];
$(function() {
$('#tree1').tree({
data: data
});
});
});
Hi, everyone. I am a total newbie in javascript. I need to print a tree on a web page. I use this widget for it. I need to write the for loop inside a variable but it is a variable so I think it is impossible. Please help me, is there a solution to my problem? The thing is that I dynamically get elements from another jsp page so the amount of children vary as well as the amount of array elements.
Upvotes: 0
Views: 2455
Reputation: 207501
To create an array of objects, change your for loop to use push().
var myArr = [];
for (i = 0; i < document.getElementById('qbeansLength').value; i++){
myArr.push( {"label" : document.getElementById('queueID'+i).value });
}
and just reference it
var data = [
{
label: ' Queues',
children: myArr
},
Upvotes: 2
Reputation: 44181
You mean something like this?
$.noConflict();
jQuery(document).ready(function($){
var data = [
{
label: ' Queues',
children: [
// Filled in below
]
},
{
label: 'node2',
children: [
{ label: 'child3' }
]
}
];
var queuesArr = new Array();
var i;
var qbeansLength = document.getElementById('qbeansLength').value;
for (i = 0; i < qbeansLength; i++) {
data[0].children.push({label: document.getElementById('queueID'+i).value});
}
$(function() {
$('#tree1').tree({
data: data
});
});
});
Upvotes: 2