Hidden/shown columns depending on whether user is logged in or not...

Hidden/shown columns depending on whether user is logged in or not...

bwlangbwlang Posts: 20Questions: 0Answers: 0
edited March 2010 in General
I have added this little block to hide/show columns with class "admin_only" (which is set during the page render)
[code]
if ($("th.admin_only").length > 0)
{
$("th").each(function(index) {
if ($(this).hasClass('admin_only'))
{
oTable.fnSetColumnVis(index,false);
}
else
{
oTable.fnSetColumnVis(index,true);
}
});
}
[/code]
unfortunately - once a column is hidden - it's always hidden, in fact it disappears from the dom during table instantiation before this code executes. If i delete the cookie, i can see it again. So... how can get this to work? Is this a bug or intended behaviour?

thanks!

Brad

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    The problem is the $('th') selector - it's looping over only the visible columns - so it will never see the hidden ones again! DataTables expects the first parameter of fnSetColumnVis to be the column index that you want to set the visibility of (regardless of column visibility) - so if you want to show/hide the first column, you would pass 0. Then the second column looks like it is the first column - but to DataTables is still the second column.

    It's a little complicated to explain by words... Experimentation is the way to go :-). One thing you can do is loop over fnSettings().aoColumns - this is an array of objects (which doesn't have it's length change on visibility!). Each object has a parameter called 'nTh' which will be the one you are interested in. This is probably the best way to go about doing what you are looking for.

    Hope this helps!
    Allan
  • bwlangbwlang Posts: 20Questions: 0Answers: 0
    Allan - many thanks!

    this worked nicely.

    [code]
    jQuery.each(oTable.fnSettings().aoColumns, function(index, col) {
    if ($(col['nTh']).hasClass('admin_only'))
    {
    oTable.fnSetColumnVis(index,false);
    }
    else
    {
    oTable.fnSetColumnVis(index,true);
    }
    });
    [/code]
This discussion has been closed.