Hide/show column dynamic

Hide/show column dynamic

fernandogferreirafernandogferreira Posts: 7Questions: 0Answers: 0
edited January 2010 in General
Hey all,

I guess I have a simple task for implementing, but I've already spent 3 hours on it. I can't find anything useful on the web... Although I am sure you will be able to help me...

I've defined a dataTable object:
[code]
oTable = $('#main').dataTable({
"bSort": [
true,
true,
true,
true,
false,
true,
true,
false,
],
"aaSorting": [ [0,'asc'], [1,'asc'] ],
"aoColumns": [
null,
null,
null,
null,
{"bSortable":false},
{"bSortable":false, "bVisible":false},
{"bSortable":false},
{"bSortable":false, "bVisible":false},
{"bVisible":false},
{"bSortable":false},
],
"fnDrawCallback":function(){
redrawTable();
}
});
[/code]

As you can see I have a Draw callback functions that handles with all table designed and operations... That's work smoothly :D

I want to be able to toggle columns dynamic inside the function drawTable depending on certain conditions...

I know that I have to use the
[code]
$.fn
[/code]

The problem is I can't find the SetColumnVis() function....

Thanks in advance...

Replies

  • TomCTomC Posts: 43Questions: 0Answers: 0
    edited January 2010
    I'm working with this right now. I had to upgrade to 1.6 I don't think it is available before that. The function is:
    fnSetColumnVis( columnIndex, false );

    I've got my columns hiding and showing but I need to force a redraw of the datatable when it gets too narrow but something is topping fnDraw from working.
  • fernandogferreirafernandogferreira Posts: 7Questions: 0Answers: 0
    But if I call a redraw inside the Draw callback function (redrawTable), it will fall into a loop without end... and how can I call fnSetColumnVis with the $.fn object?

    Can you send me a peace of code?
  • TomCTomC Posts: 43Questions: 0Answers: 0
    I missed that line. I'm not calling mine inside the draw method. I'm just giving people a control to turn on and off certain columns.
  • fernandogferreirafernandogferreira Posts: 7Questions: 0Answers: 0
    That's OK... It works for me as well... The problem is toggle columns automatically...
  • TomCTomC Posts: 43Questions: 0Answers: 0
    Can't you use whatever condition controls the columns and call it after the draw method using fnDrawCallback ?
    http://www.datatables.net/usage/callbacks
  • fernandogferreirafernandogferreira Posts: 7Questions: 0Answers: 0
    edited January 2010
    This is exactly the problem... I can't figure out how to do it...

    right now I have
    [code]
    "fnDrawCallback":function(){
    redrawTable();
    }
    [/code]

    And I would like to call SetColumnVis() inside it....
    $('tableid').SetColumnVis does not work... I almost sure that is related to $.fn object... But I don't know where in the DOM tree I can find the target method.

    Cheers
  • TomCTomC Posts: 43Questions: 0Answers: 0
    The method is not called SetColumnVis() it is called fnSetColumnVis()

    You seem to be making this harder than it has to be.

    "fnDrawCallback":function(){
    oTable.fnSetColumnVis( );
    }

    That should be the basic framework of what you need.
  • fernandogferreirafernandogferreira Posts: 7Questions: 0Answers: 0
    Sorry, I think I wasn't clear on my last posts.

    I have a fnDrawCallback function named redrawTable. Inside this function I do something like
    [code]
    $.post('checkIfUserIsLogged.php', {}, function(data){
    if(data == 'OK'){
    //The user is logged in to the system
    //Here I'd like to show some columns that are hidden for a standard visitor
    }
    });
    [/code]

    As I've told before I created the dataTable object this way:

    [code]
    oTable = $('#main').dataTable({
    "bSort": [
    true,
    true,
    true,
    true,
    false,
    true,
    true,
    false,
    ],
    "aaSorting": [ [0,'asc'], [1,'asc'] ],
    "aoColumns": [
    null,
    null,
    null,
    null,
    {"bSortable":false},
    {"bSortable":false, "bVisible":false},
    {"bSortable":false},
    {"bSortable":false, "bVisible":false},
    {"bVisible":false},
    {"bSortable":false},
    ],
    "fnDrawCallback":function(){
    redrawTable();
    }
    });
    [/code]

    oTable can't be seen inside the redrawTable. I've also tried pass $(this) to the function, but it haven't worked as well.

    I guess, somehow, I could use $.fn for calling fnSetColumnVis(...)...

    So, am I going to the wrong direction? Do you have any suggestion?
  • TomCTomC Posts: 43Questions: 0Answers: 0
    Put variables for the columns you want to show into hidden form elements that are available to the fnSetColumnVis( ) function? You don't have to write everything into the table draw function. fnSetColumnVis() just wants an integer.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Documentation: http://datatables.net/api#fnSetColumnVis
    Example: http://datatables.net/examples/api/show_hide.html

    Hopefully that will help.

    Also I hope there isn't anything in redrawTable() which will actually redraw the table! Otherwise you'll get an endless loop. fnSetColumnVis does not redraw the table, it just does some DOM manipulation.

    Regards,
    Allan
This discussion has been closed.