How to hide columns in table with special ID?

How to hide columns in table with special ID?

Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0
edited November 2015 in Free community support

I create the ".tabula" class with settings for all my tables:

$(document).ready(function() {var table = $('.tabula').DataTable( {
dom: '<"H"Bfr>t<"F"ip>',
jQueryUI: true,
buttons: [ 'colvis',
{extend: 'collection', text: 'Select Type ↓',
buttons: ['selectCells' , 'selectRows', 'selectColumns']},
'selectAll', 'selectNone', 'pageLength'],
select: {info: true, style: 'multi'},
pagingType: "full_numbers",
lengthMenu: [[ 10, 25, 50, 100, -1 ],[ '10', '25', '50', '100', 'All' ]]
});});

How to hide columns in only one table with special ID and same class name?

I tried to create the settings for this table, but they are not working. Examples:

1)

$(document).ready(function() {
$('#nas1tab2').DataTable( {
"columnDefs": [{"targets": [ -1,-2,-3 ],"visible": false}]
});});

2)

$(document).ready(function() {
$('#nas1tab2').DataTable(
buttons: {extend: 'colvis',hide: [ -1,-2,-3 ]}]
});});

3)

$(document).ready(function() {
$('#nas1tab2.tabula').DataTable( {
"columnDefs": [{"targets": [ -1,-2,-3 ],"visible": false}]
});});

4)

$(document).ready(function() {
$('#nas1tab2.tabula').DataTable( {
buttons: {extend: 'colvis',hide: [ -1,-2,-3 ]}]
});});

This question has an accepted answers - jump to answer

Answers

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    Answer ✓

    So you want to apply the same settings to any table with the class .tabula, then apply other unique settings by the table ID?

    So theres 2 ways you can go about doing this.

    1. If every table is going to have the .tabula class, then you can just set global defaults, then override the defaults as you create the table
    2. Create an object that you will use to hold the default DT values, and just extend them for each DT object, like so:
    $(document).ready( function () {
      var dtDefaults = {
        search: true,
        paging: true,
        info: false
      };
      
      var table1 = $('#example1').DataTable($.extend(dtDefaults, {
        ajax: 'http://www.justinhyland.com/p/dt/one.json.php',
        search: false
      }));
      
      var table2 = $('#example2').DataTable($.extend(dtDefaults, {
        ajax: 'http://www.justinhyland.com/p/dt/two.json.php',
        info: true
      }));
    } );
    

    JSBin

    Those are really the only way to do it. You cant assign config values by class or ID, then extend or override them, or set defaults and prioritize.

    Hope this helps

  • Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0
    edited November 2015

    Thanks a lot.
    I removed class from "#nas1tab2" table, make a little bit correction of my CSS + changed JS script, and all works fine:

    $(document).ready( function () {
    var dtDefaults = {
    dom: '<"H"Bfr>t<"F"ip>',
    jQueryUI: true,
    buttons: [ 'colvis',
    {extend: 'collection', text: 'Select Type ↓', buttons: ['selectCells' , 'selectRows', 'selectColumns']},
    'selectAll', 'selectNone', 'pageLength'],
    select: {info: true, style: 'multi'},
    pagingType: "full_numbers",lengthMenu: [[ 10, 25, 50, 100, -1 ],[ '10', '25', '50', '100', 'All' ]]
    };

    var table1 = $('.tabula').DataTable($.extend(dtDefaults, {}));
    var table2 = $('#nas1tab2').DataTable($.extend(dtDefaults, {columnDefs: [{"targets": [ -1,-2,-3 ],"visible": false}]}));
    });

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    How does this have anything to do with css?

  • Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0
    edited November 2015

    Simply, replaced color settled to the ".tabula" class to all Data Tables.
    Example.
    Changed from:

    table.dataTable.tabula tbody tr.odd {background-color: #D4CBE8;}
    table.dataTable.tabula tbody tr.even {background-color: #FAEFF6;}

    to:

    table.dataTable tbody tr.odd {background-color: #D4CBE8;}
    table.dataTable tbody tr.even {background-color: #FAEFF6;}

  • Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0
    edited November 2015

    But there is another connected question, if I stay in my JS only:

    $(document).ready( function () {
    var dtDefaults = {
    dom: '<"H"Bfr>t<"F"ip>',
    jQueryUI: true,
    buttons: [ 'colvis',
    {extend: 'collection', text: 'Select Type ↓', buttons: ['selectCells' , 'selectRows', 'selectColumns']},
    'selectAll', 'selectNone', 'pageLength'],
    select: {info: true, style: 'multi'},
    pagingType: "full_numbers",lengthMenu: [[ 10, 25, 50, 100, -1 ],[ '10', '25', '50', '100', 'All' ]]
    };

    var table1 = $('.tabula').DataTable($.extend(dtDefaults, {}));
    });

    How to add a special settings for one table exactly in html code, between style tags?:

    The versions provided below don't work:

    $(document).ready( function () {
    var table2 = $('#nas1tab2').DataTable($.extend(dtDefaults, {columnDefs: [{"targets": [ -1,-2,-3 ],"visible": false}]}));});

    $(document).ready( function () {
    $('#nas1tab2').dataTable($.extend(dtDefaults, {columnDefs: [{"targets": [ -1,-2,-3 ],"visible": false}]}));});

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75
    edited November 2015

    How to add a special settings for one table exactly in html code, between style tags?

    ..what?? I want to help, but your questions are very pourly written.

    "exactly in html code" and "between style tags" are very confusing..

    Whats the code currently look like? What are you expecting to see/happen? What is happening? Are you seeing any errors in the console? Etc etc

    Restate the question, add details, be clear, and ill be more than happy to help :-)

  • Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0
    edited November 2015

    del

  • jLinuxjLinux Posts: 981Questions: 73Answers: 75

    What????

  • Walter_StolzWalter_Stolz Posts: 46Questions: 6Answers: 0

    del = delete wrong post.

This discussion has been closed.