jquery数据表隐藏列

jQuery datatables插件有没有办法隐藏(显示)表列? 我想出了如何重新加载表数据:使用
fnClearTable
fnAddData
。 但是我的问题是,在我对该表的视图之一(例如隐藏模式)中,我不想显示某些列。     
已邀请:
您可以通过以下命令隐藏列:
fnSetColumnVis( 1, false );
其中第一个参数是列的索引,第二个参数是可见性。 通过:http://www.datatables.net/api-函数fnSetColumnVis     
如果有人再次进入这里,这对我有用...
\"aoColumnDefs\": [{ \"bVisible\": false, \"aTargets\": [0] }]
    
动态隐藏列 先前的答案使用旧版DataTables语法。在v 1.10+中,您可以使用column()。visible():
var dt = $(\'#example\').DataTable();
//hide the first column
dt.column(0).visible(false);
要隐藏多列,可以使用columns()。visible():
var dt = $(\'#example\').DataTable();
//hide the second and third columns
dt.columns([1,2]).visible(false);
这是一个小提琴演示。 初始化表时隐藏列 要在表初始化时隐藏列,可以使用columns选项:
$(\'#example\').DataTable( {
    \'columns\' : [
        null,
        //hide the second column
        {\'visible\' : false },
        null,
        //hide the fourth column
        {\'visible\' : false }
    ]
});
对于上述方法,您需要为应保持可见并且未指定其他列选项的列指定“ 7”。或者,您可以使用columnDefs定位特定的列:
$(\'#example\').DataTable( {
    \'columnDefs\' : [
        //hide the second & fourth column
        { \'visible\': false, \'targets\': [1,3] }
    ]
});
    
您可以在数据表初始化期间定义它
\"aoColumns\": [{\"bVisible\": false},null,null,null]
    
对于使用服务器端处理并将数据库值使用隐藏列传递到jQuery的任何人,我建议使用\“ sClass \”参数。您将可以使用css display:在隐藏列的同时仍然可以检索其值。 CSS:
th.dpass, td.dpass {display: none;}
在数据表init中:
\"aoColumnDefs\": [ { \"sClass\": \"dpass\", \"aTargets\": [ 0 ] } ] // first column in visible columns array gets class \"dpass\"
//编辑:记得还要将隐藏的类添加到thead单元中     
借助api,您可以使用
var table = $(\'#example\').DataTable();

table.column( 0 ).visible( false );
看这个信息: 在此处输入链接说明     
您可以尝试如下隐藏/动态显示运行时 隐藏:     fnSetColumnVis(1,false,false);      示例:oTable.fnSetColumnVis(item,false,false); 节目 : fnSetColumnVis(1,true,false);     示例:oTable.fnSetColumnVis(item,false,false); 在这里,oTable是Datatable的对象。     
如果使用json中的数据并使用Datatable v 1.10.19,则可以执行以下操作: 更多信息
$(document).ready(function() {
     $(\'#example\').dataTable( {

        columns= [
          { 
           \"data\": \"name_data\",
           \"visible\": false
           }
        ]
  });
});
    
var example = $(\'#exampleTable\').DataTable({
    \"columnDefs\": [
        {
            \"targets\": [0],
            \"visible\": false,
            \"searchable\": false
        }
    ]
});
Target属性定义列的位置。Visible属性负责列的可见性。Searchable属性负责搜索功能。如果将其设置为false,则该列不适用于搜索。     
$(document).ready(function() {
$(\'#example\').DataTable( {
    \"columnDefs\": [
        {
            \"targets\": [ 2 ],
            \"visible\": false,
            \"searchable\": false
        },
        {
            \"targets\": [ 3 ],
            \"visible\": false
        }
    ]
});});
    
注意:现在接受的解决方案已经过时,并且是旧代码的一部分。 http://legacy.datatables.net/ref 该解决方案可能不适用于使用较新版本的DataTables(现在已保留)的解决方案 对于较新的解决方案: 您可以使用: https://datatables.net/reference/api/columns().visible() 您可以实现按钮的替代方法:https://datatables.net/extensions/buttons/built-in 请查看提供的链接下的最后一个选项,该选项允许使用一个可以切换列可见性的按钮。     
希望这会帮助你。 我正在某些列上使用此解决方案进行搜索,但是我不想在前端显示它们。
$(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
                },
              ]
        });
    });
    
看我的解决方案 我有这个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>
而我的
Ajax request
返回了类似的内容 所以我想隐藏ID索引[0]和RegistrationStatusId索引[5]
$(document).ready(function() {
    $(\'#example\').dataTable( {
        \"columnDefs\": [
            { \"aTargets\": [0, 5], \"sClass\": \"invisible\"},// here is the tricky part
        ]
    });
});
希望对您有帮助     

要回复问题请先登录注册