fnShowHide to filter exported csv

fnShowHide to filter exported csv

crea2kcrea2k Posts: 14Questions: 0Answers: 0
edited December 2011 in General
Hi Is it possible to get the fnShowHide to filter the exported csv and pdf / print etc ?, I have implemeted the fnShowHide like the tutorial but when I print using tabletools or export to csv all of the data is exported, also is it possible to change sRowSelect to select columns instead, so that on export only the selected columns are exported to csv ?.
Many Thanks :-)

Replies

  • crea2kcrea2k Posts: 14Questions: 0Answers: 0
    Anyone any ideas on this ?, just being able to filter the exported csv would be good
  • crea2kcrea2k Posts: 14Questions: 0Answers: 0
    I can hide columns ok using this, but need to pass the hidden columns onto tabletools csv export button as at the moment it is just exporting the whole csv :-( , really need help on this one
  • allanallan Posts: 63,546Questions: 1Answers: 10,476 Site admin
    > sRowSelect to select columns instead

    The clue is in the parameter name :-). At some point I hope to get the chance to add column selection support to TableTools - but that is not currently implemented I'm sorry to say.

    > I can hide columns ok using this, but need to pass the hidden columns onto tabletools csv export button

    What I think you want is mColumns: http://datatables.net/extras/tabletools/button_options#mColumns . This will allow you to use visible columns only, hidden columns only, all columns, or you can specify your own array of column indexes. In theory that could actually be expanded to be a dynamic array that will allow the user to select which columns are to be exported - not something I've tried, but it should be perfectly possible by modifying that array :-)

    Allan
  • callahan09callahan09 Posts: 18Questions: 0Answers: 0
    I just wanted to write my own experience here...

    I am trying to use mColumns with an array variable to select which columns are output. I'm doing this because I need the CSV to output the visible columns only, and exclude hidden columns. But there are a couple of columns that should always be excluded, even if their columns are visible. So in the DataTable's fnDrawCallback function I am building the array (initially declared at top-level so it is available within the scope of the oTableTools options...

    So I am building my array of columns to include with the following code inside fnDrawCallback:

    vCols = new Array();

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

    So vCols basically includes all columns that are visible except it never includes column indexes 0 or 3.

    The array is built correctly, as I have verified by outputting the results separately. But when I use it as the value of my mColumns option in the oTableTools declarations, it does not work, and it simply includes all columns in the output.

    I have tested using an array variable in its simplest form just by declaring var testCols = [ 5, 6 ]; at the top-level in my javascript, then using that variable for mColumns, and it worked perfectly.

    But then if instead I just declare var testCols; (no initial values) at top-level, and assign testCols = [ 5, 6 ]; inside the fnDrawCallback function, it no longer works, despite that the DrawCallback function is called, and it is placing a value into the array variable.

    Can you help me out figuring this problem out?
    Thanks in advance for any help.

    My table implementation is found at http://collection.callahan09.com/comics
  • callahan09callahan09 Posts: 18Questions: 0Answers: 0
    To keep everyone updated, I figured out why my visibleColumns array wasn't working...

    Because the fnDrawCallback function wasn't firing until after the oTableTools initialization, so the variable was empty when I was initializing TableTools. The next thing I tried was to write a seperate function called visibleColumns() and call it for mColumns, but it gave an error because the DataTables was not finished initializing at that point. So my solution, and it's working beautifully now, was the simply initialize TableTools after DataTables is finished initializing, using the alternate initialization method for TableTools.

    http://datatables.net/release-datatables/extras/TableTools/alt_init.html

    Hopefully this helps anyone facing a similar issue!
  • callahan09callahan09 Posts: 18Questions: 0Answers: 0
    OK, well this is getting frustrating. No matter what I do to take a step towards solving this, something else always pops up as a problem. I realized that the solution I described in my previous post only works when the table is initialized. If I used ColVis to show or hide columns then click "Copy", TableTools still only copies the columns that my visibleColumns() function returned at the initialization of the document, not the new array that should be created.

    I have tried everything I can think of to solve this issue, but it just does not work.

    My best guess, but still not working, was to do the following:

    In the oColVis settings of my DataTable, I've utilized the fnStateChange function, within which I've got the following code:

    oTableTools.s.buttonSet[0].mColumns = visibleColumns();

    If I run through this javascript in debug mode, it seems to set the property just fine; it appears as though the correct work is occurring: whenever a column is made visible or hidden, the mColumns property for my "Copy" button is updated to reflect the new array of columns it should copy. But when I go to actually perform the Copy by pressing the button, it is as if it's still using the original mColumns value, not the one I've just set it to.

    Is there any solution to this?

    The only thing I can think of is to completely override the fnClick() function of the button and manually build the output with fnSetText(), which I suppose I'll try soon, but it seems odd to have to go to those lengths for this.
  • callahan09callahan09 Posts: 18Questions: 0Answers: 0
    OK! Simpler than I thought, it SEEMS to be working now as I want it to...

    Simply utilize the following in the button definition:

    "fnClick": function ( nButton, oConfig, oFlash ) {
    oConfig.mColumns = visibleColumns();
    this.fnSetText( oFlash, this.fnGetTableData(oConfig) );
    },

    Hope this helps someone if you stumble here looking for the same info.
This discussion has been closed.