Fill aaData Object with a Javascript 2D Array
Fill aaData Object with a Javascript 2D Array
Hey Guys...
Really new with datatables and have been struggling my nuts of the last 3 days trying to fill the datatable with an array. I have tried a std 1D array, a 2D array and nothing seems to work.
My data is as follows:
Title Name Surname DOB
Mrs Debbie Botha 1969-10-03
Mr Andrew Joyce 1988-06-03
Mrs Christine Louw 1960-11-26
So i comma deliminate the values so it ends up looking like this: Mrs,Debbie,Botha,1969-10-03,Mr,Andrew,Joyce,1988-06-03,Mrs Christine,Louw,1960-11-26
The code for my 2d array is as follows
[code]
var info = dbdatainformation.split("!");
var rows = info[0].split(",");
var cols = info[1].split("@");
var colnum = cols.length;
var end = (colnum - 1);
var pos = 0;
var rowsfinal = new Array;
for (i = 0; i <= (colnum - 1); i++) {
rowsfinal[i] = new Array();
var counter = 0;
for (x = pos; x <= end; x++) {
rowsfinal[i][counter] = rows[x];
counter++;
}
end = end + colnum;
pos = x;
}
[/code]
Note: 'cols' is basically my column names.
rowsfinal[0] then contains firsts rows data, rowsfinal[1] then contains second rows data, etc
How do i then pass it to a aaData?
Currently i am doing this which obviously isn't very practical.
[code]
"aaData": [
rowsfinal[0],
rowsfinal[1],
rowsfinal[2]
],
[/code]
Everything i have tried messes up the values.
Please Help... Thank you...
Really new with datatables and have been struggling my nuts of the last 3 days trying to fill the datatable with an array. I have tried a std 1D array, a 2D array and nothing seems to work.
My data is as follows:
Title Name Surname DOB
Mrs Debbie Botha 1969-10-03
Mr Andrew Joyce 1988-06-03
Mrs Christine Louw 1960-11-26
So i comma deliminate the values so it ends up looking like this: Mrs,Debbie,Botha,1969-10-03,Mr,Andrew,Joyce,1988-06-03,Mrs Christine,Louw,1960-11-26
The code for my 2d array is as follows
[code]
var info = dbdatainformation.split("!");
var rows = info[0].split(",");
var cols = info[1].split("@");
var colnum = cols.length;
var end = (colnum - 1);
var pos = 0;
var rowsfinal = new Array;
for (i = 0; i <= (colnum - 1); i++) {
rowsfinal[i] = new Array();
var counter = 0;
for (x = pos; x <= end; x++) {
rowsfinal[i][counter] = rows[x];
counter++;
}
end = end + colnum;
pos = x;
}
[/code]
Note: 'cols' is basically my column names.
rowsfinal[0] then contains firsts rows data, rowsfinal[1] then contains second rows data, etc
How do i then pass it to a aaData?
Currently i am doing this which obviously isn't very practical.
[code]
"aaData": [
rowsfinal[0],
rowsfinal[1],
rowsfinal[2]
],
[/code]
Everything i have tried messes up the values.
Please Help... Thank you...
This discussion has been closed.
Replies
[code]
var a = [
[ 'Mr', 'Andrew', 'Joyce', '1988-06-03' ],
[ 'Mrs', 'Christine', 'Louw', '1960-11-26' ]
];
[/code]
So if you have comma separated values in a string you need to convert them into an array, which you can do simply using the 'split' operator: string.split(',');. So you need to do that in a loop to build a 2D array.
Allan
Forgive me for being stupid but that is what i did in the JS above in my frist post if i'm no mistaken?
The issue is how do I then write the whole 2D Array to the "aaData" object?
I am currently doing this but if i don't know how many objects in the array it will be a problem.
[code]
"aaData": [
rowsfinal[0],
rowsfinal[1],
rowsfinal[2]
],
[/code]
I want to do something like this:
[code]
"aaData": [
rowsfinal
]
[/code]
rowsfinal being my 2d array. I cannot pass the array directly i have to specify the each row to add. (rowsfinal[0]) but if i don't know how many rows there are what do i do?
After having another read throu some of the posts i realized what i was doing wrong.
it should read:
[code]
"aaData": rowsfinal
[/code]
Thanks for your help.