Bug when using custom sSortDataType

Bug when using custom sSortDataType

stottenstotten Posts: 4Questions: 0Answers: 0
edited July 2013 in Bug reports
I believe either the examples for sSortDataType are wrong or there is a bug, however it seems that if I use a custom sorting preprocessor the value returned in the array can be written into the wrong aoData location.
I believe the sort handler should take as input the data in the order it is presented in the table (i.e. using a DOM query such as

[code]$( 'td:eq('+iColumn+')', oSettings.oApi._fnGetTrNodes(oSettings) ).each( function () { ... } )[/code]

However this results in the datatables library writing the returned value into the location at

[code]_fnSetCellData( oSettings, j, iColumn, aData[j] );[/code]

If my data is already sorted (ie I've clicked sort already), then subsequent calls to the sort handler will result in the above "j" writing to the wrong location, i.e. as if my sSortDataType was querying the original unsorted data.

I believe the fix for this is to change the above _fnSetCellData call to

[code]_fnSetCellData( oSettings, oSettings.aiDisplayMaster[j], iColumn, aData[j] );[/code]

This takes the sorted order into account. An alternative fix would instead be to insist that all sSortDataType implementations instead sort the data based upon aoData (e.g. using_fnGetCellData) - this would then always use the original unsorted data rather than the displayed data.

An example of the bug can be seen at http://jsfiddle.net/knChb/1/

Replies

  • allanallan Posts: 56,506Questions: 1Answers: 8,970 Site admin
    Can you link me to a test case which doesn't work please? This example uses _fnGetTrNodes and does work: http://datatables.net/release-datatables/examples/plug-ins/dom_sort.html .

    Allan
  • stottenstotten Posts: 4Questions: 0Answers: 0
    Hi Allan,

    The JSFiddle link I included in the original post should show this issue - if you run this and sort by the first column the first time it works (as the displayed data and sorted data are in the same order), but subsequent sorts start getting out of order.

    Please let me know if you have any issues.

    Thanks
  • allanallan Posts: 56,506Questions: 1Answers: 8,970 Site admin
    Oops sorry - I completely misunderstood! Yes, I see the problem. Its actually not a DataTables bug, but just something that has changed in newer jQuery versions. They change the ordering of elements in the selector, so:

    [code]
    $( 'td:eq('+iColumn+')', oSettings.oApi._fnGetTrNodes(oSettings) )
    [/code]

    Gives the nodes in the order of the document - not the original array, which is what we want.

    This is a change to use $.map which addresses the issue:
    http://jsfiddle.net/knChb/2/

    Allan
  • stottenstotten Posts: 4Questions: 0Answers: 0
    Great - thank you
This discussion has been closed.