Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Sorting on every column except the first one

i'm currently using Datatables for a Custom system and i would like to disable Sort for every column but the first one.

I tried with the following code wich is working fine when i add values separated by comma

"aoColumnDefs": [
    { 'bSortable': false, 'aTargets': [ 1, 2, 3, 4 ] }
],

But my tables column number vary for each individual file so i can have 3 or maybe 12 columns, and i don't want to have to manually add the values for each file.

If i add more values than the columns i have in one file i get the following error, and an execution stop

Uncaught TypeError: Cannot read property 'className' of undefined

So, is there any way i can get those index and pass them to the function?

Thanks!

like image 580
Danny22 Avatar asked Dec 12 '13 15:12

Danny22


2 Answers

You can add a specific class to the TH element that you do not want to be sortable.

<table>
   <thead>
      <th>
         ...
      </th>
      <th class="no-sort">
         ...
      </th>
    </thead>
    <tbody>
      ...
    </tbody>
</table>

And then you can specify this class in your aTargets parameter.

"aoColumnDefs": [
    { 'bSortable': false, 'aTargets': ['no-sort'] }
]

View here for more information on the Column specific options.

like image 186
Nunners Avatar answered Sep 18 '22 22:09

Nunners


And then you can specify this class in your aTargets parameter.

columnDefs: [ { orderable: false, targets: [1,2,3,4,5,6,7,8,9] } ]
like image 21
Anas Naguib Avatar answered Sep 19 '22 22:09

Anas Naguib