I need a datatable with column sorting using the YUI library. So I am trying to adapt this example:
YUI().use("datatable-sort", function(Y) {
var cols = [
{key:"Company", label:"Click to Sort Column A", sortable:true},
{key:"Phone", label:"Not Sortable Column B"},
{key:"Contact", label:"Click to Sort Column C", sortable:true}
],
data = [
{Company:"Company Bee", Phone:"415-555-1234", Contact:"Sally Spencer"},
{Company:"Acme Company", Phone:"650-555-4444", Contact:"John Jones"},
{Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
],
table = new Y.DataTable({
columns: cols,
data : data,
summary: "Contacts list",
caption: "Table with simple column sorting"
}).render("#sort");
});
I need to dynamically add the data into this table. So I passed a two-dimensional array from the server-side to the JS function. It is an array consisting of arrays which represent rows, then the data inside the inner arrays represents the table's cells data. The array on the server-side was:
Array ( [0] => Array ( [0] => Name [1] => Age [2] => CGPA ) [1] => Array ( [0] => Alex [1] => 23 [2] => 2.5 ) [2] => Array ( [0] => Bob [1] => 24 [2] => 3 ) [3] => Array ( [0] => Mike [1] => 22 [2] => 3.9 ) )
This is something I was trying to do (see the for
loop to put data in the cols =[...]
) but it does not work, not even the alert($rowsArray);
part. Aparently this function does not seem to work at all.
M.mod_quiz.init_tabView = function(Y, params) {
var $rowArray= params.key1;
alert($rowArray);
YUI().use("datatable-sort", function(Y) {
var cols = [
for (i=0; i<$rowArray.length; i++) {
{key:"Column "+i , label: $rowArray[0][i] , sortable=true}
}
/*{key:"Company", label:"Sort Column A", sortable:true},
{key:"Phone", label:"Sort Column B", sortable:true},
{key:"Contact", label:"Sort Column C", sortable:true}*/
],
data = [
{Company:"Company Bee", Phone:"415-555-1234", Contact:"Sally Spencer"},
{Company:"Acme Company", Phone:"650-555-4444", Contact:"John Jones"},
{Company:"Industrial Industries", Phone:"408-555-5678", Contact:"Robin Smith"}
],
table = new Y.DataTable({
columns: cols,
data : data,
summary: "Contacts list",
caption: "Table with simple column sorting"
}).render(params.key2);
});
};
QUESTION:
So how can I dynamically load the data into this datatable from the array?