Sorting not working for dynamically generated columns

Sorting not working for dynamically generated columns

sumeetbsumeetb Posts: 3Questions: 0Answers: 0
edited December 2010 in General
Hi,

I am working on a "Table Maintenance" module, where a set of tables names will be displayed in the left pane, and clicking on a table name, all records from that table will displayed in the right pane.

hence i am generating my s dynamically. Also "aoColumns" for DataTable is being generated dynamically:
[code]
for (i = 0; i < listColumnMaintenance.length; i++) {
columnMaintenance = listColumnMaintenance[i];

if (columnMaintenance.IsUpdatable) {
tableHeadersHtml += '' + columnMaintenance.ColumnAlias + '';
}
else {
tableHeadersHtml += '' + columnMaintenance.ColumnAlias + '';
}
aoColumns[i] = { "bSortable": true };
}
[/code]

Finally, I am constructing my DataTable:
[code]
datatableTableData = $('#grdTableData').dataTable({
"aoColumns": aoColumns,
"iDisplayLength": 5,
"bProcessing": true,
"bDestroy": true,
"bRetrieve": true,
"bPaginate": true,
"aaSorting": [[totalNumberOfColumns - 1, 'desc']],
"sPaginationType": "two_button",
"oLanguage": {
"sEmptyTable": "No matching records found"
},
"aaData": JSON.parse(jsonResult),
"fnRowCallback": function (nRow, aData, iDisplayIndex) {
for (columnIndex = 0; columnIndex < totalNumberOfColumns; columnIndex++) {
if ($('th:eq(' + columnIndex + ')').attr('class').indexOf('updatable') >= 0) {
$('td:eq(' + columnIndex + ')', nRow).addClass('updatable');
$('td:eq(' + columnIndex + ')', nRow).addClass('widthAuto');
$('td:eq(' + columnIndex + ')', nRow).addClass('heightAuto');
}
}
return nRow;
}
});
[/code]

Now, when the page is rendered, everything else works fine, but column sorting does not work at all.
Its not adding the "sorting" class to s.

Please help!

Replies

  • allanallan Posts: 61,880Questions: 1Answers: 10,139 Site admin
    Are you getting any errors or anything in the console? In principle (as long as you insert the TH elements, with a TR, into the THEAD for the table before you initialise DataTables) I don't see any problem with doing what you are. Can you confirm that the structure being created is what DataTables needs: http://datatables.net/usage/#prerequisites .

    Allan
  • sumeetbsumeetb Posts: 3Questions: 0Answers: 0
    Thanks Allan.

    That was indeed the problem. I was not constructing my header properly.
    Now it works perfectly fine!

    Thanks again.
This discussion has been closed.