Using Gson, using array, not working?
Using Gson, using array, not working?
Hello,
I am a noob, using GSON.toJson on my server, so I have some work to do before using DataTables.
The data returned from JQuery will be in the form of objects with properties. Thus, I created arrObjects to simulate the data returned. If I set arrObjects or arrData for aaData, I still have the same issue:
I have a sample below. The problem is, it is iterating through each "row" and placing it in the first row's columns.
[code]
$(document).ready(
function() {
var arrObjects = new Array();
arrObjects[arrObjects.length] = new Object();
arrObjects[arrObjects.length-1]["A"]="hi";
arrObjects[arrObjects.length-1]["B"]="world";
arrObjects[arrObjects.length] = new Object();
arrObjects[arrObjects.length-1]["A"]="hi2";
arrObjects[arrObjects.length-1]["B"]="world2";
var arrData = new Array();
for ( var i = 0; i < arrObjects.length; i++) {
arrData[arrData.length] = "[\"" + arrObjects[i].A
+ "\",\"" + arrObjects[i].B + "\"]";
}
alert(arrData);
$("#eventTable").dataTable( {
"aaData" : [ eval(arrData) ],
"aoColumns" : [ {
"sTitle" : "A"
}, {
"sTitle" : "B"
} ]
});
});
[/code]
I've also tried this:
[code]
for ( var i = 0; i < arrObjects.length; i++) {
arrData[arrData.length] = new Array();
arrData[arrData.length -1][0] = arrObjects[i].A;
arrData[arrData.length -1][1] = arrObjects[i].B;
}
[/code]
I expect:
A B
hi world
hi2 world2
I see:
A B
hi,world hi2,world2
Any suggestions?
Thanks!
Eric
I am a noob, using GSON.toJson on my server, so I have some work to do before using DataTables.
The data returned from JQuery will be in the form of objects with properties. Thus, I created arrObjects to simulate the data returned. If I set arrObjects or arrData for aaData, I still have the same issue:
I have a sample below. The problem is, it is iterating through each "row" and placing it in the first row's columns.
[code]
$(document).ready(
function() {
var arrObjects = new Array();
arrObjects[arrObjects.length] = new Object();
arrObjects[arrObjects.length-1]["A"]="hi";
arrObjects[arrObjects.length-1]["B"]="world";
arrObjects[arrObjects.length] = new Object();
arrObjects[arrObjects.length-1]["A"]="hi2";
arrObjects[arrObjects.length-1]["B"]="world2";
var arrData = new Array();
for ( var i = 0; i < arrObjects.length; i++) {
arrData[arrData.length] = "[\"" + arrObjects[i].A
+ "\",\"" + arrObjects[i].B + "\"]";
}
alert(arrData);
$("#eventTable").dataTable( {
"aaData" : [ eval(arrData) ],
"aoColumns" : [ {
"sTitle" : "A"
}, {
"sTitle" : "B"
} ]
});
});
[/code]
I've also tried this:
[code]
for ( var i = 0; i < arrObjects.length; i++) {
arrData[arrData.length] = new Array();
arrData[arrData.length -1][0] = arrObjects[i].A;
arrData[arrData.length -1][1] = arrObjects[i].B;
}
[/code]
I expect:
A B
hi world
hi2 world2
I see:
A B
hi,world hi2,world2
Any suggestions?
Thanks!
Eric
This discussion has been closed.
Replies
[code]
$(document).ready(function() {
var arrObjects = new Array();
var d = new Date();
arrObjects[arrObjects.length] = new SliceObject();
arrObjects[arrObjects.length - 1]["A"] = "hello";
arrObjects[arrObjects.length - 1]["B"] = "world";
arrObjects[arrObjects.length - 1]["C"] = d;
arrObjects[arrObjects.length - 1].prep();
arrObjects[arrObjects.length] = new SliceObject();
arrObjects[arrObjects.length - 1]["A"] = "hello2";
arrObjects[arrObjects.length - 1]["B"] = "world2";
arrObjects[arrObjects.length - 1]["C"] = d;
arrObjects[arrObjects.length - 1].prep();
$("#eventTable").dataTable( {
"aaData" : arrObjects,
"aoColumns" : [ {
"sTitle" : "A"
}, {
"sTitle" : "B"
}, {
"sTitle" : "C"
} ]
});
});
function SliceObject() {
this.length = 0;
this.arr = new Array();
this.slice = function() {
return this.arr.slice();
}
this.prep = function() {
for (prop in this) {
if (typeof (this[prop]) != "function" && prop != "length"
&& prop != "arr") {
this.arr[this.length] = this[prop];
this[this.length] = this[prop];
this.length++;
}
}
length -= 1;
};
}
[/code]