fnSort: oSettings.aoColumns[iColumn] is undefined

fnSort: oSettings.aoColumns[iColumn] is undefined

dforrestdforrest Posts: 2Questions: 0Answers: 0
edited March 2011 in General
Allen -

Thank you for the great work.

I am having a problem with fnSort. The basic set-up is as follows:

HTML table structure:

[code]



ID
Name
Active
Edit
Delete





[/code]

Javascript:

[code]
var clTable;
clTable = $('#clients_tbl').dataTable({
"bJQueryUI": true,
"bAutoWidth": false,
"bLengthChange": false,
"bPaginate": true,
"sPaginationType": "full_numbers",
"bScrollCollapse": false,
"iDisplayLength": 20,
"bSort": false,
"aoColumns":[
{"sWidth": "10%"},
{"sWidth": "75%"},
{"sWidth": "5%"},
{"sWidth": "5%"},
{"sWidth": "5%"}
],
"aoColumnDefs": [
{ "sSortDataType": "dom-text", "aTargets": [ 1 ] },
{ "sType": "string", "aTargets": [ 1 ] }
]
});

function add_test() {
clTable.fnAddData([
"stuff 1",
"stuff 2",
"stuff 3",
"stuff 4",
"stuff 5"
]);
clTable.fnSort([1, 'asc']);
}
[/code]

When the table is populated and add_test is called, fnAddData works perfectly but fnSort results in a "oSettings.aoColumns[iColumn] is undefined" error as shown in firebug. I am using version 1.7.6 (with jquery 1.5.1) and the error is coming from line 4389:

[code]var sDataType = oSettings.aoColumns[ iColumn ].sSortDataType;[/code]


I have tried putting the sSortDataType and sType in all the aoColumn defs instead of in aoColumnDefs, but got the same result.
Puzzled. What am I missing here?

-Doug Forrest

Replies

  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Hi Doug,

    fnSort takes a 2D array (http://datatables.net/api#fnSort - so multiple column sorting is possible) - as such you want to give fnSort in this case is:

    [code]
    clTable.fnSort([[1, 'asc']]);
    [/code]
    Doesn't fnAddData do the sort for you automatically (if the user is already sorting on that column of course)?

    Allan
  • dforrestdforrest Posts: 2Questions: 0Answers: 0
    Thanks, Allan, that did the trick!

    At the movement, all the existing data is being loaded pre-sorted and user-sorting is disabled, so fnAddData doesn't do any sorting.
  • allanallan Posts: 63,516Questions: 1Answers: 10,473 Site admin
    Ah I see - fair enough. Good to here that did the job for you :-)

    Allan
This discussion has been closed.