jquery datatables hide column

前端 未结 13 1328
灰色年华
灰色年华 2020-12-02 11:59

Is there a way with the jquery datatables plugin to hide (and show) a table column?

I figured out how to reload the table data: using fnClearTable and <

相关标签:
13条回答
  • 2020-12-02 12:31
    var example = $('#exampleTable').DataTable({
        "columnDefs": [
            {
                "targets": [0],
                "visible": false,
                "searchable": false
            }
        ]
    });
    

    Target attribute defines the position of the column.Visible attribute responsible for visibility of the column.Searchable attribute responsible for searching facility.If it set to false that column doesn't function with searching.

    0 讨论(0)
  • 2020-12-02 12:31

    take look at my solution

    I have this HTML table Head

    <thead>
        <tr>
            <th style="width: 20%">@L("Id")</th>
            <th style="width: 20%">@L("IdentityNumber")</th>
            <th style="width: 20%">@L("Name")</th>
            <th style="width: 20%">@L("MobileNumber")</th>
            <th style="width: 20%">@L("RegistrationStatus")</th>
            <th style="width: 20%">@L("RegistrationStatusId")</th>
            <th style="width: 20%; text-align: center;" data-hide="phone">@L("Actions")</th>
        </tr>
    </thead>
    

    and my Ajax request returned something like this

    so I want to hide Id index [0] and RegistrationStatusId index [5]

    $(document).ready(function() {
        $('#example').dataTable( {
            "columnDefs": [
                { "aTargets": [0, 5], "sClass": "invisible"},// here is the tricky part
            ]
        });
    });
    

    I hope this would help you

    0 讨论(0)
  • 2020-12-02 12:34

    Note: the accepted solution is now outdated and part of legacy code. http://legacy.datatables.net/ref The solutions might not be appropriate for those working with the newer versions of DataTables (its legacy now) For the newer solution: you could use: https://datatables.net/reference/api/columns().visible()

    alternatives you could implement a button: https://datatables.net/extensions/buttons/built-in look at the last option under the link provided that allows having a button that could toggle column visibility.

    0 讨论(0)
  • 2020-12-02 12:37

    If use data from json and use Datatable v 1.10.19, you can do this:

    More info

    $(document).ready(function() {
         $('#example').dataTable( {
    
            columns= [
              { 
               "data": "name_data",
               "visible": false
               }
            ]
      });
    });
    
    0 讨论(0)
  • 2020-12-02 12:38

    You can hide columns by this command:

    fnSetColumnVis( 1, false );
    

    Where first parameter is index of column and second parameter is visibility.

    Via: http://www.datatables.net/api - function fnSetColumnVis

    0 讨论(0)
  • 2020-12-02 12:47

    Hide columns dynamically

    The previous answers are using legacy DataTables syntax. In v 1.10+, you can use column().visible():

    var dt = $('#example').DataTable();
    //hide the first column
    dt.column(0).visible(false);
    

    To hide multiple columns, columns().visible() can be used:

    var dt = $('#example').DataTable();
    //hide the second and third columns
    dt.columns([1,2]).visible(false);
    

    Here is a Fiddle Demo.

    Hide columns when the table is initialized

    To hide columns when the table is initialized, you can use the columns option:

    $('#example').DataTable( {
        'columns' : [
            null,
            //hide the second column
            {'visible' : false },
            null,
            //hide the fourth column
            {'visible' : false }
        ]
    });
    

    For the above method, you need to specify null for columns that should remain visible and have no other column options specified. Or, you can use columnDefs to target a specific column:

    $('#example').DataTable( {
        'columnDefs' : [
            //hide the second & fourth column
            { 'visible': false, 'targets': [1,3] }
        ]
    });
    
    0 讨论(0)
提交回复
热议问题