DataTables 1.9.4: Can't Sort (With Test Case)

DataTables 1.9.4: Can't Sort (With Test Case)

machineghostmachineghost Posts: 9Questions: 0Answers: 0
edited April 2013 in General
My issue is pretty simple: no matter what options I enable or disable, I can't seem to get DataTables to sort my data. It "focuses" on the column when I click it, the arrow changes from up to down when I click it, and the aria-sort attribute also changes ... but the table doesn't actually sort.

I created a test case here:
http://live.datatables.net/utuxil/edit#preview

If you scroll to the "expression value" column (or just enter some values in a different column) and try clicking on the header you can see the problem. I've tried ... well it feels like I've tried every sorting option in the list, but none have helped. I even made a second test case to show that the two most obvious ones (sType and sSortDataType) don't help:

http://live.datatables.net/exaceq/edit#preview

I imagine I must be missing just one key option (how many ways can there be to make DataTables do *everything* involved in sorting except actually sort?) ... does anyone out there have any idea what that one key option might be?

Replies

  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Thank you for the test case.

    The key thing here to know is that sorting in DataTables occurs (usually) on the values which are read when the row is added to the table (in this case at initialisation time). It does not reevaluate the HTML for sorting on each sort as that could be horrifically slow.

    However, sometimes that is needed, as is the case of your input and select elements here. For that you need to use live DOM sorting. There is an example of how to do that here: http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html .

    Allan
  • machineghostmachineghost Posts: 9Questions: 0Answers: 0
    Thanks, that worked great! It did feel a bit hacky though; perhaps a future version of DataTables might bottle all that up in to a single "sUseLiveData" option?

    I did have one problem though: I discovered I had to add the following also:

    $.fn.dataTableExt.afnSortData['std'] = function(oSettings, iColumn) {
    return $.map(oSettings.oApi._fnGetTrNodes(oSettings), function(tr, i) {
    return $('td:eq(' + iColumn + ')', tr).text();
    });
    };

    Without it, my non-input/non-select (ie. plain text) columns wouldn't sort. Perhaps that should be added to the example code?

    Either way, thanks again.
  • allanallan Posts: 63,522Questions: 1Answers: 10,473 Site admin
    Good to hear that worked.

    Yes it is a bit of a workaround at the moment - I'm still working on how best to integrating it directly into DataTables without increasing the size of the library significantly, while retaining flexibility.

    Allan
This discussion has been closed.