Default sorting

Default sorting

miltontmiltont Posts: 22Questions: 8Answers: 0

I understand that you can alter the Default ordering by adding:

$('#example').DataTable({
order:[[3, 'desc'], [0, 'asc']]
});

But is there a way to alter to original order so that if a user selects a header it always sorts in descending order rather than in ascending order as happens now.

I have a lot of columns that are numeric which hold a lot of empty or zero values and the users have to click the header twice: once to sort in the default ascending order and the second time to sort in the descending order.

Ideally the first column would sort ascending as it is alphabetical, but all other columns should sort descending as they are numerical.

Milton.

Answers

  • kthorngrenkthorngren Posts: 21,318Questions: 26Answers: 4,948

    See if columns.orderSequence does what you want.

    Kevin

  • miltontmiltont Posts: 22Questions: 8Answers: 0

    Thanks again Kevin,

    I added the order sequence as suggested into the columnDefs, but I am not getting the correct sort order.

    I assume it is because My required order when the user first views the Table is set in "order" and then "order sequence" is after this?

    I tried moving the ColumnDefs above the "order" line but was getting a syntax error.

    <script>
                    $(document).ready( function () {
                    $('#myTable').dataTable( {                   
                            "order":[[1,"desc"],[2,"desc"],[3,"desc"],[4,"desc"],[5,"desc"]],   //Specifies Column to use for sorting 0 = 1st Column and direction = descending - make sure you add a comma at the end
                            "dom": '<lif<t>p>',                                                 //Adds the Total Players to the middle
                            lengthMenu: [[200,100,25,50,-1],[200,100,25,50,"All"]],             //First row of Numbers is what is shown initiallly, Second row of Numbers is what is in the Drop Down Menu selector
                            fixedHeader: {headerOffset: 80},                                    //Makes the Header fixed                           
                            columnDefs: [                                                       //This makes the column align to the following
                                { targets: [1,2,3,7,8], className: 'dt-body-center'},
                                { targets: [4,5,6,9], className: 'dt-body-right'},
                                { targets: [1], render: DataTable.render.number( ',', '.' )},           //Add a thousand seperator
                                { targets: [7], render: DataTable.render.number( ',', '.', 0, '$' )},   //Add a thousand seperator and $ sign
                                { "orderSequence": [ "asc" ], "targets": [ 0 ] },                       //set the starting Order
                                { "orderSequence": [ "asc" ], "targets": [ 1,2,3,4,5,6,7,8,9,10,11,12,13 ] }
                            ]
                            
                        }); 
                    } );
                </script>
    

    Milton.

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

Sign In or Register to comment.