BillNathan
BillNathan

Reputation: 609

How to disable sorting of first column of DataTables?

I have tried many ways of disabling sorting on the first column. I have not been able to achieve it.

I have been able to disable all columns beside the first column using:

'columnDefs': [ {
'targets': [],
'orderable': false, // set orderable false for selected columns
}]

Can anyone help me, please?

Upvotes: 0

Views: 4788

Answers (2)

Mayuresh Srivastava
Mayuresh Srivastava

Reputation: 1422

order is used to apply initial order (sort) to the table.
It can be used to apply initial sorting on any other column.
By default, DataTables use first column even if you disable sorting for that column using targets.

Upvotes: 0

Avanthika
Avanthika

Reputation: 4182

Your columnDefs.targets is empty. You have to pass the index of the column where you want to disable sorting. For first column, you need to pass the first column's index, 0.

As per datatables documentation, you can disable ordering of first column in two ways:

  1. With columnDefs:
$('#example').dataTable( {
  "columnDefs": [
    { "orderable": false, "targets": 0 }
  ]
} );
  1. With columns:
"columns": [
    { "orderable": false },
    null,
    null,
    null,
    null
]

Upvotes: 2

Related Questions