So I have a datatable:
$(tables[i]).DataTable({
paging: false,
searching: false,
info: false,
ordering: true,
autoWidth: false,
columns: [ ... column stuff here ...
{name: "Name"},
{name: "Account"},
{name: "Number"}
]
});
later in code, I watch for a click event on a button so that I can grab some data from the table and then sort by a column
var columnName = $('.mySelectBox').val();
var columnNumber = 0;
if(columnName === "Account")
columnNumber = 1;
var table = $(tables[i]).DataTable();
I would like to now sort by either column 0 or column one on this button click. But not on any other column.
//this doesn't work for me
table.sort( [ [columnNumber, 'desc'] ] );
The existing answers are using legacy DataTables syntax. Versions 1.10+ should use the following syntax: $('table'). dataTable({ "pageLength": -1, //display all records "order": [[ 0, "desc" ]] // Sort by first column descending });
As you would expect with a desktop application, DataTables allows you to sort by multiple columns at the same time. This multiple sorting mechanism is always active if the bSort initialiser is true (it is by default) and the end user can activate it by 'shift' clicking on the column they want to add to the sort.
date format should be YYYY-MM-DD. sort it *"order": [[ 3, "desc" ]] * and hide the td, th display none.
I use .order()
instead of .sort()
. Example:
$('#dataTables-example').DataTable().order([0, 'desc']).draw();
where 0
is the id of the column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With