How to use the API?

How to use the API?

PC-GramPC-Gram Posts: 5Questions: 2Answers: 0
edited January 2020 in Free community support

I simply want the first table column initially to be sorted DESC instead of the default ASC. But how do I combine

<script>
$(document).ready( function () {
$('#example').DataTable()  }
</script>

with this form the api order() examples:

var table = $('#example').DataTable();
table.sort( [ 0, 'desc' ] );
table.draw()

I need an example that shows how these two elements are combined into one <script>-section

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,735

    You can use the order option to set the initial ordering of the Datatable.

    There is not a sort() API. The API is order().

    Kevin

  • PC-GramPC-Gram Posts: 5Questions: 2Answers: 0

    Having had lunch I found both the error and the solution:

    <script>
    $(document).ready( function () {
    var table = $('#example').DataTable();  
    table.order( [ 0, 'desc' ] );
    table.draw()
    }
    </script>
    
  • colincolin Posts: 15,112Questions: 1Answers: 2,583
    edited January 2020

    Yep, you can also chain methods together, so something like:

    $('#example').DataTable().order( [ 0, 'desc' ] ).draw();
    

    or

    var table = $('#example').DataTable(); 
    table.order( [ 0, 'desc' ] ).draw()
    

    Colin

This discussion has been closed.