Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
up vote 47 down vote accepted

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

share|improve this answer
2  
I really wish the datatables API site put each method on a different page -- it makes it so much easier to Google for a particular one. – Blazemonger Feb 7 '13 at 21:19
    
@Blazemonger: there are anchors, for easy access: datatables.net/api#fnSetColumnVis – Robot Mess Sep 30 '13 at 11:15
    
@RobotMess Anchors are fine for humans to link to, but Google search results only go to the top of the page. – Blazemonger Sep 30 '13 at 12:49

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

"aoColumnDefs": [{ "bVisible": false, "aTargets": [0] }]
share|improve this answer
    
U win bra, ddnt knw that aTargets is an array – Sboniso Marcus Nzimande Dec 7 '15 at 7:44

You can define this during datatable initialization

"aoColumns": [{"bVisible": false},null,null,null]
share|improve this answer
    
"aoColumns": [{"bVisible": false}] was sufficient for me (without nulls). – Gerry Gurevich Jan 15 '15 at 9:01
    
@Gerry_Gurevich is partly correct. In this example the first column gets the property "bVisible": false while all other columns in the table are not passed any arguments. This is because when you use "aoColumns": [ ... ] you have to send a comma separated list of ALL columns in the table. If you use "aoColumnDefs": [ ... ] instead (see @ahaliav_fox's answer) you only need declare an array of column indexes to apply the property to. Therefore you don't need to declare the state of every column whether it gets the property or not. – DrewT Mar 25 '15 at 7:32

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

share|improve this answer

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] }
    ]
});
share|improve this answer
    
It could also be done with columns.visible option. If you want your answer to be thorough, I would mention that as well. – Gyrocode.com Jul 27 at 16:41
    
@Gyrocode.com - updated. – devlin carnate Jul 27 at 17:11
    
That's great update, but I've meant the option columns,visible, see datatables.net/reference/option/columns.visible – Gyrocode.com Jul 27 at 17:32
1  
@Gyrocode.com - aha, yes. I was thinking dynamic visibility. I've added detail about hiding columns on init. – devlin carnate Jul 27 at 17:50
    
Great answer, thanks for the update! – Gyrocode.com Jul 27 at 21:24

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.

share|improve this answer
$(document).ready(function() {
$('#example').DataTable( {
    "columnDefs": [
        {
            "targets": [ 2 ],
            "visible": false,
            "searchable": false
        },
        {
            "targets": [ 3 ],
            "visible": false
        }
    ]
});});
share|improve this answer

take look at my solution

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

I hope this would help you

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.