jquery datatables hide column

前端 未结 13 1326
灰色年华
灰色年华 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:24

    For anyone using server-side processing and passing database values into jQuery using a hidden column, I suggest "sClass" param. You'll be able to use css display: none to hide the column while still being able to retrieve its value.

    css:

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

    In datatables init:

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

    //EDIT: remember to add your hidden class to your thead cell also

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

    if anyone gets in here again this worked for me...

    "aoColumnDefs": [{ "bVisible": false, "aTargets": [0] }]
    
    0 讨论(0)
  • 2020-12-02 12:27
    $(document).ready(function() {
    $('#example').DataTable( {
        "columnDefs": [
            {
                "targets": [ 2 ],
                "visible": false,
                "searchable": false
            },
            {
                "targets": [ 3 ],
                "visible": false
            }
        ]
    });});
    
    0 讨论(0)
  • 2020-12-02 12:28

    Hope this will help you. I am using this solution for Search on some columns but i don't want to display them on frontend.

    $(document).ready(function() {
            $('#example').dataTable({
                "scrollY": "500px",
                "scrollCollapse": true,
                "scrollX": false,
                "bPaginate": false,
                "columnDefs": [
                    { 
                        "width": "30px", 
                        "targets": 0,
                    },
                    { 
                        "width": "100px", 
                        "targets": 1,
                    },
                    { 
                        "width": "100px", 
                        "targets": 2,
                    },              
                    { 
                        "width": "76px",
                        "targets": 5, 
                    },
                    { 
                        "width": "80px", 
                        "targets": 6,
                    },
                    {
                        "targets": [ 7 ],
                        "visible": false,
                        "searchable": true
                    },
                    {
                        "targets": [ 8 ],
                        "visible": false,
                        "searchable": true
                    },
                    {
                        "targets": [ 9 ],
                        "visible": false,
                        "searchable": true
                    },
                  ]
            });
        });
    
    0 讨论(0)
  • 2020-12-02 12:29

    With the api you can use

    var table = $('#example').DataTable();
    
    table.column( 0 ).visible( false );
    

    Look this info:

    enter link description here

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

    You can try as below to hide/show dynamically runtime

    Hide : fnSetColumnVis( 1, false, false );

    Example: oTable.fnSetColumnVis(item, false,false);

    Show : fnSetColumnVis( 1, true, false );

    Example: oTable.fnSetColumnVis(item, false,false);

    Here, oTable is object of Datatable.

    0 讨论(0)
提交回复
热议问题