Exporting data with Buttons and Responsive extensions, controlled by Column visibility.
Exporting data with Buttons and Responsive extensions, controlled by Column visibility.
I would like to combine Column visibility with Exporting and Responsiveness.
I have a large table with 15 columns. I would like to export only the selected ones. The problem is, some of the selected columns are invisible due to Responsiveness.
My goal:
Export to CSV the selected cols (random 10 are selected).
Results:
Default options:
All of the 15 columns are exported.
Columns: ':visible'
Only the first 5 selected is exported, the following 5 are hidden because of responsiveness.
How can I make Datatable export recognise the selected columns with "Column visibility"?
Thanks,
Blitz
Answers
This example shoes using column selectors for exporting:
https://datatables.net/extensions/buttons/examples/html5/columns.html
It shows using an array of columns. But it also references using
column-selector. The choice of whichcolumn-selectoris dependent on your environment.Kevin
So the key to my problem will be the appropriate use of
column-selector.How can I select only the columns enabled by
Column visibility?If I export with
:visible, it hides some of the columns enabled byColumn visibility, because it is physically hidden byResponsiveextensionOkay, Im getting closer.
I was able to export columns set visible by Column visibility but hidden by Responsiveness.
Now I have to make it clearer and global by leaving out
$('#products').DataTable()and using something which refers to the current table.have you found a solution?
I figured out a way to do it. The id of the table is reflected into the node tag if you are using aria controls.
I think this is actually a bug, since column visibility and responsiveness are two different things, with different application scenarios.
A column being hidden due to the size of the viewport should have nothing to do whether or not it is part of the export to Excel.
Hi @Joyrex ,
As shown in this example here, Responsive and the Excel export work fine together - all columns are exported regardless of the Responsive hiding. The issue with this thread is when you add column visibility into the mix.
Cheers,
Colin
@colin,
I figured out what was causing my issue - on my Excel
exportOptions, I hadvisible: trueset, so it was excluding the columns that were not visible due to Responsive kicking in. It forced me to not use the column visibility plugin due to this.Any solution on it? It's a problem also for me.... I need to export only columns affected by Column visibility to true, also if my responsiveness hide me it.
For example: I set column visibility to true on columns: 0,1,2,3,4,5,6
Column 7 is always show with noVis class (it contain buttons) and I don't want to export it (column 7 with class .noVis and .actions).
But my screen is small and I can see only columns 0,1,2,3,7
When I click Export I want to export all columns 0,1,2,3,4,5,6 (but not 7).
Using this:
exportOptions: {
columns: [ ':visible:not(.actions)' ]
}
doesn't work cause responsiveness
Hi @itajackass ,
This thread was resolved, there was a solution. If you're seeing problems, could you link to a test case please that demonstrates the problem. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.
Cheers,
Colin
hi @colin ,
of course here my example: http://live.datatables.net/dofezuxa/3/edit
If I try to remove :visible from the exportOptions I get exported always ALL column even if I chose to hide some columns by COLUMN VISIBILITY plugin.
Hi @itajackass ,
I combined @montulli 's fix with your test case, to give this here. The key is to make the
buttons.exportData()a bit smarter -Cheers,
Colin
Hi @colin , thanks for the reply! Your solution works as expected. I've only a question: cause i've to insert this fix on all my pages with datatables....about 40 pages... and each tables has a different name instead "example".
Is there a global code instead: return $('#example').DataTable().column(idx).visible();
For example: return $('this').DataTable().column(idx).visible(); ?
Thanks
Hi @itajackass ,
It looks like
thisis the window object, so that's no good, but if the table being export is the only table on the page, you could useCheers,
Colin
Hi @colin, no, sometimes there are multiple table in the same page...
i've tried also: return $(node).closest("table").DataTable().column(idx).visible(); with no lucky.
Let me know if there's a solution or i've to copy/paste name of id table in each of my pages. Thanks
Hi @itajackass ,
Yep, if it's not visible, it won't be in a table so you need to do this:
Cheers,
Colin