Default sort order when using DataTables?

Default sort order when using DataTables?

longknot52longknot52 Posts: 6Questions: 2Answers: 0

How can I specify a default sort order of my recordset when using DataTables? Right now, the default sort order of the query is being ignored in my output.

Thank you.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Use the order option to set the default order. If this doesn't help then please post the Datatables init code showing the default order you want.

    Kevin

  • longknot52longknot52 Posts: 6Questions: 2Answers: 0

    I found this info, but when I use this code in my page, I get an error.

    $(document).ready(function() {
        $('#example').DataTable( {
            "order": [[ 3, "desc" ]]
        } );
    } );
    

    DataTables warning: table id=example - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3

    I can't figure out why it's not working.

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    Did you follow the troubleshooting steps in the link ( http://datatables.net/tn/3 )?

    Kevin

  • longknot52longknot52 Posts: 6Questions: 2Answers: 0

    Yes, but I didn't see anything to do with a sort error. I just want it to sort by my first column in descending order as a default, so I tried this, and got the error.

    $(document).ready(function() {
        $('#example').DataTable( {
            "order": [[ 1, "desc" ]]
        } );
    } );
    
  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    The error means that Datatables has been initialized elsewhere in your code, ie, $('#example').DataTable( ...} );. If this is the case move the "order": [[ 1, "desc" ]], into that initialization. If you still need help then we will need to see the problem by looking at a link to your page or a test case replicating the issue.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • longknot52longknot52 Posts: 6Questions: 2Answers: 0

    Kevin, thank you so much! Now I understand. I am using an included file in my file, and calling it there as well (I forgot about that!). It's working as expected now.

    Thank you!!! :D

This discussion has been closed.