Columndefs not working Datatables

12,986

Solution 1

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 )

Solution 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.

Solution 3

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

columns: [
          targets: [0],
          visible: false
        ]
Share:
12,986
user2611745
Author by

user2611745

Updated on June 25, 2022

Comments

  • user2611745
    user2611745 almost 2 years

    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.

    • engineersmnky
      engineersmnky over 9 years
      have you tried "columnDefs":[{visible: false, "targets":0}] or "columns": [{visible: false}] as specified in the Reference Docs
    • engineersmnky
      engineersmnky over 9 years
      I just noticed a syntax error in my comment visible should be "visible"
    • user2611745
      user2611745 over 9 years
      I've already tried columns and columndefs. Did not help. The quotes do not help as well since this is a Coffeescript file.
    • engineersmnky
      engineersmnky over 9 years
      the problem is your syntax you are correct the quotes are wrong for coffeescript but you need to fix your syntax for this to work.
    • user2611745
      user2611745 over 9 years
      What needs to be corrected with the syntax again ?
    • engineersmnky
      engineersmnky over 9 years
      columns:[targets:[0],visible:false] is incorrect try columns:[{visible:false}] and see if that works
    • dougajmcdonald
      dougajmcdonald about 8 years
      really interested to know if you got columns and columnDefs working together, I'm seeing a similar problem
  • davaus
    davaus almost 5 years
    This is the ONLY option that worked for me. Thankyou