DataTables Set Default sorting column and set unsortable columns
SET INITIAL ORDER (DataTables 1.10)
Use order
to set initial order of the table.
For example, to sort by second column in descending order:
$('#example').dataTable({
"order": [[ 1, 'desc' ]]
});
See this jsFiddle for code and demonstration.
DISABLE SORTING FOR A COLUMN (DataTables 1.10)
Use columnDefs
and orderable
to disable sorting on certain columns.
For example, to disable sorting on third and forth columns:
$('#example').dataTable({
"columnDefs": [
{ "targets": [2,3], "orderable": false }
]
});
See this jsFiddle for code and demonstration.
SET INITIAL ORDER AND DISABLE SORTING FOR THE SAME COLUMN (DataTables 1.10)
You can combine order
option to set initial order of the table and orderable
to disable sorting on the same column.
For example:
$('#example').dataTable({
"order": [[ 0, 'desc' ]],
"columnDefs": [
{ "targets": [0], "orderable": false }
]
});
See this jsFiddle for code and demonstration.
You can do this via the data-order
data attribute on the table HTML which will give you the flexibility you need on a table by table basis while still allowing you to use a single call to initialise your dataTables:
<table className="table table-condensed table-striped" data-order="[[ 2, "asc" ]]" id="tableId">
<thead>
<tr>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
<th>Col4</th>
<th>Col5</th>
<th>Col6</th>
</tr>
</thead>
<tbody>
<tr>
<td>Val1</td>
<td>Val2</td>
<td>Val3</td>
<td>Val4</td>
<td>Val5</td>
<td>Val6</td>
</tr>
</tbody>
</table>
Just Include the following code:
$(document).ready(function() {
$('#tableID').DataTable( {
"order": [[ 3, "desc" ]]
} );
}
);
Reference:
https://datatables.net/examples/basic_init/table_sorting.html
As per the table sorting docs you can do that using the order
option:
$('.table-asc0').dataTable({
order: [[0, 'asc']]
})
The 0 indicates to sort on the first column, while asc
to do it in ascending order. You can chose any other column and use desc
too.
For DataTables versions prior to 1.10
you should use aaSorting
instead
$('.table-asc0').dataTable({
aaSorting: [[0, 'asc']]
})
To order descending on the first column:
$('.table-asc1').dataTable({
aaSorting: [[1, 'desc']]
})