jQuery DataTables hide column without removing it from DOM

后端 未结 5 1885
囚心锁ツ
囚心锁ツ 2020-12-30 01:22

I need to hide a column from showing up in jquery datatables. When I hide the column using bVisible property it disappears from the DOM.

I want to set display proper

相关标签:
5条回答
  • 2020-12-30 01:35

    You should use className along with the columnDefs or the columns,

    Define hide_column class in your css like this

    .hide_column {
        display : none;
    }
    

    You have two ways to assign that .hide_column class:

    Use columnDefs (assign custom class to first column):

    $('#example').DataTable( {
      columnDefs: [
        { targets: [ 0 ],
          className: "hide_column"
        }
      ]
    } );
    

    OR columns

    $('#example').DataTable( {
      "columns": [
        { className: "hide_column" },
        null,
        null,
        null,
        null
      ]
    } );
    

    code snippets taken from here


    Old answer

    Try adding

    "sClass": "hide_column"
    

    that should make that column hidden...

    0 讨论(0)
  • 2020-12-30 01:38

    You can use the method hide.

    $(element).hide();
    

    To show the element, use the method show:

    $(element).show();
    

    To get the column that you want, you can use n-th child selector from jquery.

    0 讨论(0)
  • 2020-12-30 01:42

    If you want to hide multiple columns:

    $('#example').dataTable({
      "columnDefs": [{ 
        "targets": [0,1,3], //Comma separated values
        "visible": false,
        "searchable": false }
      ]
    });
    
    0 讨论(0)
  • 2020-12-30 01:45

    To build on Daniel's answer:

    css:

    th.hide_me, td.hide_me {display: none;}
    

    In datatables init:

    "aoColumnDefs": [ { "sClass": "hide_me", "aTargets": [ 0 ] } ] // first column in visible columns array gets class "hide_me"
    

    Remember to add your hidden class to your thead cell also:

    <thead>
        <th class="hide_me">First Column</th>
        <th>Second Column</th>
        <th>Third Column</th>
    </thead>
    

    This is also a useful strategy if you're using server-side processing and want to pass in data from the ajax source without it being visible in the datatable. You'll still be able to retrieve the column's value on the front-end without needing to display it. Helpful for filtering via hidden data values etc.

    Example:

    // In datatables init file
    <script>
        var filteredValues = [];
        $('td.your_filtering_class').each(function(){
            var someVariable = $(this).find('.hide_me').html();
            filteredValues.push(someVariable);
        }
    </script>
    
    0 讨论(0)
  • 2020-12-30 01:47

    this is my contribution for u.

    Not sure if the code is correct but its work.

    If u do have more than one setup column like me.

    $('#example').dataTable( {
      "columnDefs": [ {
            "targets"  : 'no-sort',
            "orderable": false,
            "order": [],
        }],
        "columnDefs": [ {
            "targets"  : 'hide_column',
            "orderable": false,
            "order": [],
            "visible": false
        }]
    } );
    
    0 讨论(0)
提交回复
热议问题