Disable Sorting on every column except the first o

2020-07-10 10:35发布

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!

3条回答
贼婆χ
2楼-- · 2020-07-10 11:07

This worked for me and seems more practical (though not exactly elegant)

columnDefs: [
  {
    "targets": [0],
    "orderable": true
  }, {
    "targets": [''],
    "orderable": false
  }
]
查看更多
ゆ 、 Hurt°
3楼-- · 2020-07-10 11:10

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

columnDefs: [ { orderable: false, targets: [1,2,3,4,5,6,7,8,9] } ]

查看更多
Melony?
4楼-- · 2020-07-10 11:25

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.

查看更多
登录 后发表回答