colVis - get list of hidden or shown columns?

colVis - get list of hidden or shown columns?

bill.martinobill.martino Posts: 38Questions: 0Answers: 0
edited December 2010 in Plug-ins
Is there a method to return a list of which columns are hidden or shown via the colVis plug-in?

I need to save the user's settings to a database so need to have a list of column names.

Replies

  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    You can get this information from the internal information in aoColumns. Loop over that and grab the bVisible property (i.e. oTable.fnSettings().aoColumn[ i ].bVisible). This could be made up into a plug-in API function which might be quite nice.

    Allan
  • bill.martinobill.martino Posts: 38Questions: 0Answers: 0
    This works great! thank you again!

    also, for archival sakes, i had to use something along the lines of

    oTable.fnSettings().aoColumns[ i ].bVisible

    'aoColumns' instead of 'aoColumn'
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Oops :-) Thanks for the correction.

    Allan
  • bill.martinobill.martino Posts: 38Questions: 0Answers: 0
    edited December 2010
    I've got an array of visible fields being built using

    "fnInfoCallback": function() {

    vCols = new Array();

    $.each(oTable.fnSettings().aoColumns, function(c){
    if(oTable.fnSettings().aoColumns[c].bVisible == true){
    vCols = vCols.concat(oTable.fnSettings().aoColumns[c].sName)
    }
    });

    }

    However I am unsure how to push this list to the ajax call. I tried

    "fnServerData": function ( aoData){
    aoData.push(vCols);
    }

    but it did not work, can you provide further guidance as to how to get this data to be included in the other variables sent to the ajax call?

    I am using server-side processing and my ajax source is defined

    I am receiving "vCols is not defined" because obviously the function does not know what my list variable is -

    thanks!
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    You can add HTTP variables to fnServerData like this: http://datatables.net/examples/server_side/custom_vars.html . You'll probably want to 'join' your array since it needs to be text you send to the server.

    Allan
  • bill.martinobill.martino Posts: 38Questions: 0Answers: 0
    edited December 2010
    I am able to make the passing of extra variables work if I give it a data set as in the example. However, my issue is that the vCols array I am creating in the fnInfoCallback section is not available to the fnServerData section. How can I make the variable available to the other part of the code?

    Sorry to be a pest, but I have been plugging at this all last night to no avail.

    thanks!
  • allanallan Posts: 63,687Questions: 1Answers: 10,500 Site admin
    Can you not just create it in fnServerData as well? Or perhaps stick it into it's own function and call that function from both fnInfoCallback or fnServerData as needed?

    Allan
  • bill.martinobill.martino Posts: 38Questions: 0Answers: 0
    Of course!

    After much trial and error, this is what worked for me:

    // get visible columns
    $.fn.getColumnsShown = function(dTable)
    {
    vCols = new Array();

    $.each(dTable.fnSettings().aoColumns, function(c){
    if(dTable.fnSettings().aoColumns[c].bVisible == true){
    vCols = vCols.concat(dTable.fnSettings().aoColumns[c].sName)
    }
    });

    return vCols;
    }

    $(document).ready(function(){


    // data table
    var oTable = $('#resultTable').dataTable({

    "fnServerData": function ( sSource, aoData, fnCallback ) {

    columnsShown = $('#resultTable').getColumnsShown(this);

    /* Add some extra data to the sender */
    aoData.push( { "name": "visColumns", "value": columnsShown } );
    $.getJSON( sSource, aoData, function (json) {
    /* Do whatever additional processing you want on the callback, then tell DataTables */
    fnCallback(json)
    } );
    }

    etc...

    etc...
    });
    });

    thanks again! Hope this may help someone in the future
This discussion has been closed.