ADD Order by id desc

ADD Order by id desc

orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

Hello all.

I need to order table by id. ID is not shown in table, how can I add order by data from mysql? Say simply i need to select my table by this query "Select comun1,column2,...,column(N) from MY_TABLE order by ID desc". Where and how to do it?

Thank you for your reply and help.

This question has an accepted answers - jump to answer

Answers

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    I would send back the data in the right order, then, in your datatable add order:[], that will keep the order from changing until a user clicks on a column

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    @bindrid Dear bindrid. Can you write me small example how to do it? I do not know how to do it by Editor

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    Try this, got to live.http://live.datatables.net/.net , it is a sandbox that Allen has set up for us to play in.

    If you run the code as is, you will see it default to the first column in asc order.
    Now change the code from

      var table = $('#example').DataTable();
    
    

    to

      var table = $('#example').DataTable({order:[[2,"asc"]]});
    
    

    This will change the order column to the 3 column, asc.

    now change it to

      var table = $('#example').DataTable({order:[]});
    
    

    This will cause the order to be set in the order the data is fed to it. No column ordering icons will be set since no order is being set. In this case, it will be in the order that the html was created.

    here is the documentation https://datatables.net/reference/option/order

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2
    edited May 2017

    @bindrid Dear Bindrid. Thank you for reply, somehow it is working. I need to "order by id desc" and i am adding "order:[[0,"desc"]]". But problem is that i did not add id to columns but I added it as primary key when generated datatable. Now it takes 0 as first column from datatble( not need column )

  • kthorngrenkthorngren Posts: 21,141Questions: 26Answers: 4,918
    edited May 2017 Answer ✓

    I do this with most all of my tables. The first column is the ID column but its hidden by Datatables using the following:

            columnDefs: [
                {   "targets": [0], 
                    "visible": false,
                    "searchable": false 
                },
    

    Then you should be able to use your order config.

    Kevin

  • orik3ll0orik3ll0 Posts: 36Questions: 12Answers: 2

    @kthorngren thank you!) It works now!)

This discussion has been closed.