Amadeu Cabanilles
Amadeu Cabanilles

Reputation: 963

JQuery / Datatable set order By in specific column

I have this datatable defined but by default is ordered by the first column, and I want it ordered by the second column

 $(document).ready(function(){
                $('[data-toggle="tooltip"]').tooltip();
                var table = $('#producersTableId').DataTable({
                    "dom": '<"top">rt<"bottom"lp><"clear">',
                    "autoWidth": false,
                    "columnDefs": [
                        {"targets": [0], "width": '20%'},
                        {"targets": [1], "width": '20%'},
                        {"targets": [2], "width": '35%'},
                        {"targets": [3], "width": '15%'},

                    ]
                });
                table.columns().every( function () {
                    var that = this;
                    $( 'input', this.header() ).on( 'keyup change', function () {
                        if ( that.search() !== this.value ) {
                            that
                                .search( this.value )
                                .draw();
                        }
                    } );
                } );
            });

Upvotes: 1

Views: 48

Answers (1)

codtex
codtex

Reputation: 6548

you need to specify 'order' property:

$(table).DataTable({
    order: [[1, 'asc']]  // where the number is index of the column (i think zero besed)
    // so in your case second column is with index 1 
});

Check this out https://datatables.net/examples/basic_init/table_sorting.html

Upvotes: 1

Related Questions