columns.visible(!columns.visible()); not working as epxected

columns.visible(!columns.visible()); not working as epxected

mihomesmihomes Posts: 165Questions: 23Answers: 0
edited April 2014 in DataTables 1.10
Having an issue with toggling visibility of columns. When using the below the 1st column hides and the 2nd stays visible. Any clicks after this initial one do nothing. The same setup just toggling only one column performs as expected. Is this because I am using the same data value in both just rendering it differently? I am using server-side.

[code]
columns 1 and 2 respectively (using same data value from server-side) :

{
"data": "window_title",
"render": function ( data, type, row ) {
return '';
}
},
{
"data": "window_title",
"render": function ( data, type, row ) {
return ''+data+'';
}
},

toggle function :

// toggle ss view from thumbnail / list view
$('#dtToggle').on( 'click', function (e) {
e.preventDefault();

// Get the column API object
var columns = dt.columns( 1,2 );

// Toggle the visibility
columns.visible(!columns.visible());
});
[/code]

Changing the toggle function to only show/hide one column works perfectly fine though :

[code]
// toggle ss view from thumbnail / list view
$('#dtToggle').on( 'click', function (e) {
e.preventDefault();

// Get the column API object
var column = dt.columns( 1 );

// Toggle the visibility
column.visible(!column.visible());
});
[/code]

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    > columns.visible()

    The API docs ( http://next.datatables.net/reference/api/columns().visible() ) say the return value is:

    > API instance with the result set containing a boolean value for each column the selector matched. The boolean values indicate: true if the column is visible, false if it is not.

    So the return value is always going to be an API instance, what changes is the result set.

    Therefore:

    > !columns.visible()

    Is _always_ going to give you `false` since, even an empty result set, is a valid object.

    Allan
This discussion has been closed.