jquery数据表隐藏列

发布于 2024-11-01 03:08:31 字数 162 浏览 2 评论 0原文

jquery datatables 插件有办法隐藏(和显示)表列吗?

我弄清楚了如何重新加载表数据:使用 fnClearTablefnAddData

但我的问题是,在我的表视图之一(例如隐藏模式)中,我不想显示某些列。

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 fnAddData.

But my issue is that in one of my views for the table (e.g. a hidden mode) I don't want to show certain columns.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

温暖的光 2024-11-08 03:08:31

动态隐藏列

前面的答案使用旧版 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);

这是一个小提琴演示

初始化表时隐藏列

要在初始化表时隐藏列,可以使用 option:

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

对于上述方法,您需要为应保持可见且未指定其他列选项的列指定 null。或者,您可以使用 columnDefs 来定位特定列:

$('#example').DataTable( {
    'columnDefs' : [
        //hide the second & fourth column
        { 'visible': false, 'targets': [1,3] }
    ]
});

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] }
    ]
});
以为你会在 2024-11-08 03:08:31

您可以通过此命令隐藏列:

fnSetColumnVis( 1, false );

其中第一个参数是列的索引,第二个参数是可见性。

通过:http://www.datatables.net/api - 函数fnSetColumnVis

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

第几種人 2024-11-08 03:08:31

如果有人再次进入这里,这对我有用......

"aoColumnDefs": [{ "bVisible": false, "aTargets": [0] }]

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

"aoColumnDefs": [{ "bVisible": false, "aTargets": [0] }]
给我一枪 2024-11-08 03:08:31

您可以在数据表初始化期间定义它

"aoColumns": [{"bVisible": false},null,null,null]

You can define this during datatable initialization

"aoColumns": [{"bVisible": false},null,null,null]
风筝有风,海豚有海 2024-11-08 03:08:31

对于任何使用服务器端处理并使用隐藏列将数据库值传递到 jQuery 的人,我建议使用“sClass”参数。您将能够使用 css display: none 隐藏该列,同时仍然能够检索其值。

css:

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

在数据表 init 中:

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

//编辑:记得将隐藏类也添加到 thead 单元格中

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

甩你一脸翔 2024-11-08 03:08:31

通过 API,您可以使用

var table = $('#example').DataTable();

table.column( 0 ).visible( false );

查看此信息:

在此处输入链接说明

With the api you can use

var table = $('#example').DataTable();

table.column( 0 ).visible( false );

Look this info:

enter link description here

妥活 2024-11-08 03:08:31

如果使用 json 中的数据并使用 Datatable v 1.10.19,您可以执行以下操作:

更多信息

$(document).ready(function() {
     $('#example').dataTable( {

        columns= [
          { 
           "data": "name_data",
           "visible": false
           }
        ]
  });
});

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
           }
        ]
  });
});
傲鸠 2024-11-08 03:08:31
var example = $('#exampleTable').DataTable({
    "columnDefs": [
        {
            "targets": [0],
            "visible": false,
            "searchable": false
        }
    ]
});

Target 属性定义列的位置。Visible 属性负责列的可见性。Searchable 属性负责搜索功能。如果设置为 false,则该列无法进行搜索。

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.

幼儿园老大 2024-11-08 03:08:31

您可以尝试如下来动态隐藏/显示运行时

隐藏
fnSetColumnVis( 1, 假, 假 );

示例: oTable.fnSetColumnVis(item, false,false);

显示
fnSetColumnVis( 1, true, false );

示例: oTable.fnSetColumnVis(item, false,false);

这里,oTable是Datatable的对象。

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.

乙白 2024-11-08 03:08:31

注意:接受的解决方案现已过时,并且是遗留代码的一部分。 http://legacy.datatables.net/ref
这些解决方案可能不适合那些使用较新版本的 DataTables 的人(现在是旧版本)
对于较新的解决方案:
你可以使用:
https://datatables.net/reference/api/columns().visible( )

您可以实现按钮的替代方案: https://datatables.net/extensions/按钮/内置
查看提供的链接下的最后一个选项,该选项允许有一个可以切换列可见性的按钮。

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.

迟月 2024-11-08 03:08:31

希望这会对您有所帮助。
我正在使用此解决方案在某些列上进行搜索,但我不想在前端显示它们。

$(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
                },
              ]
        });
    });

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
                },
              ]
        });
    });
离线来电— 2024-11-08 03:08:31

对于自动目标始终错误样式

        order: [],
        columns: [
            {data: "no", orderable: false},
            {data: "photo" },
            {data: "full_name" },
            {data: "nik" },
            {data: "position" },
            {data: "created_at"},
            {data: "action", className:"text-center"},
        ]

数据表 Codeigniter 4

For automatically target always error style

        order: [],
        columns: [
            {data: "no", orderable: false},
            {data: "photo" },
            {data: "full_name" },
            {data: "nik" },
            {data: "position" },
            {data: "created_at"},
            {data: "action", className:"text-center"},
        ]

datatables codeigniter 4

も星光 2024-11-08 03:08:31
$(document).ready(function() {
$('#example').DataTable( {
    "columnDefs": [
        {
            "targets": [ 2 ],
            "visible": false,
            "searchable": false
        },
        {
            "targets": [ 3 ],
            "visible": false
        }
    ]
});});
$(document).ready(function() {
$('#example').DataTable( {
    "columnDefs": [
        {
            "targets": [ 2 ],
            "visible": false,
            "searchable": false
        },
        {
            "targets": [ 3 ],
            "visible": false
        }
    ]
});});
一片旧的回忆 2024-11-08 03:08:31

看看我的解决方案

我有这个 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 请求 返回类似这样的内容

在此处输入图像描述

所以我想隐藏 ID 索引 [0 ] 和 RegistrationStatusId 索引 [5]

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

​​ 我希望这对您有帮助

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

enter image description here

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文