fnSortListener

fnSortListener

danideideadanideidea Posts: 6Questions: 0Answers: 0
edited January 2011 in Bug reports
Hi Allan,

I'm trying to use fnSortListener to use the callback that provides, but the click only sort in an ascendent way: no matter how many times you click, it always sort ascendently, and arrow stays the same.

What I've done is something like this: oTable.fnSortListener(document.getElementById("th_type"), 4). I don't know if is that enough or I'm missing something.

Thanks.
Regards.

Replies

  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    Hi danideidea,

    I've just tried the following on my zero config example ( http://datatables.net/examples/basic_init/zero_config.html ) and it seems to operate as expected with the following code:

    [code]
    $(document).ready(function() {
    var oTable = $('#example').dataTable();
    oTable.fnSortListener( $('.full_width')[0], 1 );
    } );
    [/code]
    Could you give us a link to your page where it isn't working?

    Allan
  • danideideadanideidea Posts: 6Questions: 0Answers: 0
    No, sorry, I can't do that in this moment.

    But, the first parameter, the node, it's a reference to th in the datatable, isn't it? I've got this:



    ...
    ...
    Type
    ...
    ...



    And my datatable's definition:

    var oTable = $('#table').dataTable({
    "sPaginationType": "full_numbers",
    "bAutoWidth": false,
    "bProcessing": true,
    "bJQueryUI": true,
    "bPaginate": false,
    "bFilter": true,
    "bInfo": true,
    "aaSorting": [[5,'asc']],
    "sDom": "<'H'fl>T<'clear'>rt<'F'ip>",
    "aoColumns": [
    ...
    ...
    { "sWidth": null, "bSortable": true }, //type
    ...
    ...
    ],

    "fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
    nRow.setAttribute("id", aData[1]);
    // Bold the grade for all 'A' grade browsers
    if ( aData[0] == "10" ) {
    $(nRow).addClass("A");
    } else if (aData[0] == "20") {
    $(nRow).addClass("B");
    } else if (aData[0] == "30") {
    $(nRow).addClass("C");
    }

    return nRow;
    }
    });
    new FixedHeader( oTable );

    And then the call to fnSortListener (inside document ready):

    oTable.fnSortListener($("#th_type")[0], 4, function() {
    alert(oTable.fnSettings().aaSorting);
    });

    That returns always the same (4,desc,1) and I don't know why.

    D'you see something weird?

    Thanks again.
  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    Ah I see - no it's not a reference to the TH element - it's a reference to whatever you want to make clickable. If you are attaching it to a TH node which already has one of the build in sort listeners on it, you will actually be sorting the table twice - which is why it always appears to have the same sorting order (one click to reverse, and then reverse again to the original order).

    So do you need to add the sort listener at all? I'd guess that the TH already one on it.

    Allan
  • danideideadanideidea Posts: 6Questions: 0Answers: 0
    What I really need to do is to change data before the sort takes place, and then sort it. And I was trying to catch the click event over the th and change it with the callback. Is there any way to do it?

    Thank you.
  • electricowlelectricowl Posts: 2Questions: 0Answers: 0
    Re @danidea's previous comment.

    I have what sounds like a similar scenario where I have a column that needs to be treated as numeric. However it also contains cells with 'n/a' in them. Instead of being sorted together they are ignored in the sort so I get.

    1,000
    1,100
    n/a
    1,105
    1,200

    instead of
    n/a
    1,000
    1,100
    1,105
    1,200

    [quote]danideidea said: What I really need to do is to change data before the sort takes place, and then sort it.[/quote]

    Is there a setting I've missed or do I need to write a custom function as @danidea would like to?
  • allanallan Posts: 61,821Questions: 1Answers: 10,127 Site admin
    You need to use a formatted number sorter like this one: http://datatables.net/plug-ins/sorting#formatted_numbers , since your numbers contain non-numeric data in them.

    Allan
  • electricowlelectricowl Posts: 2Questions: 0Answers: 0
    Thanks Allan for the super speedy response!
This discussion has been closed.