DataTables Secondary Sorting on column name click

DataTables Secondary Sorting on column name click

michaelk46michaelk46 Posts: 7Questions: 3Answers: 1

I am trying to do a secondary sort of column 1, if the 13th column is hit on a page but not interrupt the functioning of any other clicked on column.

This is the jQuery DataTable code

$('#table-report').dataTable({
            "aLengthMenu": [[10, 25, 50, 100], [10, 25, 50, 100]],
            "iDisplayLength": 10,
            "bFilter": true,
            "bInfo": false,
            "sEmptyTable": "No data returned for values input.",
            "sDom": '<top>l',
            "sPaginationType": "full_numbers",
            "bJQueryUI": false,
            "aoColumnDefs":
             [
                { 'bSortable': true, "aTargets": [0, 17] },
                { "sType": "time-us", "aTargets": [16] }
             ],
            "oLanguage": {
                "sLengthMenu": "View Per Page _MENU_ ",
                "sSearch": "Search all columns:"
            }
        });

This closest I have gotten is adding

{ targets: [13], orderData: [13, 1] }
to "aoColumnDefs". The problem is that the sort (which happens correctly - Primarily and Secondarily) happens on page load and disables the ability to sort by any other column.

Does someone know how I can alter what I have to do the secondary sort only when the 13th column header is clicked on, while not disabling the ability of sorting by any other column by clicking the appropriate column headers?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Can you link to a page showing the problem please. I don't see why including that code would disable the sort on other columns.

    Allan

  • michaelk46michaelk46 Posts: 7Questions: 3Answers: 1
    edited May 2015

    No, unfortunately not. Let me see if I can set something upon jsfidldle

  • michaelk46michaelk46 Posts: 7Questions: 3Answers: 1
    edited May 2015

    Allan,

    I am still trying to get something together for you to look at (busy day). I saw something that looks promising though . With this line in aoColumnDefs:

    { "iDataSort": 1, "aTargets": [13] }
    

    It sorts on column 1 when I click column 13. Is there a way to modify the above statement to sort on both column 13 and then 1?

  • michaelk46michaelk46 Posts: 7Questions: 3Answers: 1
    Answer ✓

    Allan,

    I finally got it....

    I ended adding an id to the Route column header and calling fnsort manually on click:

        $('#dataTableId').fnSort([[13, 'asc'], [1, 'asc']])
    
  • allanallan Posts: 63,679Questions: 1Answers: 10,498 Site admin

    Good ot hear you got it working - thanks for posting back.

    Allan

This discussion has been closed.