4

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.

user2611745
  • 61
  • 1
  • 1
  • 7

3 Answers3

6

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 )
Robbie
  • 61
  • 1
  • 5
2

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.

Andrew
  • 789
  • 7
  • 13
1

Maybe you have a missing comma after targets: [0]

columns: [
          targets: [0],
          visible: false
        ]
benjamingranados
  • 438
  • 5
  • 15