How can I change the column which is being ordered?

How can I change the column which is being ordered?

lokkolokko Posts: 3Questions: 1Answers: 0

I have a table with three columns, which is currently being sorted ascending in the first column. However, I would instead like to be sorting descending by the third column (index 2) when the webpage loads. I've had a look around but none of the solutions have worked for me, most notably using:

new DataTable('#example', {
order: [[2, 'desc']]
});

Any help would be apprecaited.

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,545Questions: 26Answers: 4,818

    Your code snippet works in this tets case:
    https://live.datatables.net/yonotuki/1/edit

    Please provide a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kthorngrenkthorngren Posts: 20,545Questions: 26Answers: 4,818
    Answer ✓

    Some of the things I can think that could be a problem:

    1. That piece of code is not executed.
    2. The column is ordered but not in the order you expect.
    3. You are populating the HTML table after Datatables initializes - Datatables won't know about this.

    Kevin

  • lokkolokko Posts: 3Questions: 1Answers: 0

    Thanks for your reply @kthorngren ! I know this is very inconvenient but I unfortunately cannot share the code. The sort of thing I was looking for was some line of code which I could change in the jquery.dataTables.js file to make this happen (downloaded from the project builder).

  • allanallan Posts: 62,211Questions: 1Answers: 10,205 Site admin

    We can't really give you that code or line of code, since the example that Kevin gave you shows it working as expected. That suggests that the is a problem unique to the page you have which we would need to be able to see to be able to debug.

    Most likely it is a configuration issue, or perhaps you have serverSide enabled and the server-side script doesn't correctly implement ordering?

    Can you use the debugger to give me a trace please - click the Upload button and then let me know what the debug code is. That might give me enough of a clue to see what is going wrong.

    Allan

  • lokkolokko Posts: 3Questions: 1Answers: 0
    edited December 2023

    Apologies, I've now managed to get it working thanks to the help I've received! Thanks again!

  • kthorngrenkthorngren Posts: 20,545Questions: 26Answers: 4,818
    edited December 2023 Answer ✓

    Have you tried this?

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

    It is not expected or recommended to modify the jquery.Datatables.js file to apply configurations.

    Kevin

Sign In or Register to comment.