Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Columndefs not working Datatables

I am trying to work with columndefs to add custom colors to a column. But i started with a simple scenario just to get columndefs to work. But i am unable to do so.

This is my CoffeeScript file.

jQuery ->
      $('#clients').dataTable
        columns: [
          targets: [0]
          visible: false
        ]
        sAjaxSource: $('#clients').data('source')

I have a ROR application. i am using jquery-datatables-rails gem version: 2.2.3 https://github.com/rweng/jquery-datatables-rails which installs the latest version of datatables - 1.10.

I am not sure why i am unable to set visibility for column zero to be false.

like image 542
user2611745 Avatar asked Sep 12 '25 13:09

user2611745


2 Answers

The format you originally used is for 'columnDefs', with 'column' you have to specify for all columns see examples.

However I've always had issues with both formats not working so a workaround is to dynamically hide the column after initializing it using:

$('#clients').DataTable().column( 0 ).visible( false )
like image 146
Robbie Avatar answered Sep 14 '25 02:09

Robbie


As Robbie said in his answer, the options given for columns in your example are actually the options meant for columnDefs. However, it should still work since "visible" is a valid property for both columns and columnDefs. Since column options are index based, the "targets" property is just ignored.

I had issues with column visibility as well until I realized that the stateSave option overrides column visible. Though you don't set stateSave in your example, the only reason I can think of for column visible to not work is having stateSave set to true.

You can override the stateSave logic by using the stateSaveParams callback detailed here.

like image 37
Andrew Avatar answered Sep 14 '25 02:09

Andrew