Giving users the option of not having headers when exporting to Excel or CSV
Giving users the option of not having headers when exporting to Excel or CSV
Test case at http://live.datatables.net/lulopoku/17
We need the ability for users to export data into Excel and CSVs and give them the option of including the header or not. In the test case above, if you click the Options button, you'll get a dialog that gives users the option of whether to include the header or not. You then click the Excel, CSV or TSV buttons to get download the files.
My question is how do we go about doing that?
This question has an accepted answers - jump to answer
Answers
Use your variable
displayHeaders
as the value for theheaders
option. For example:http://live.datatables.net/lulopoku/18/edit
I set the Excel button to use the variable and set the variable to
false
for demonstration.Your code for the
option
button has some errors, see the browser's console. Once you work that out you should be able to dynamically choose the header export.Kevin
Thanks for the quick reply. In the Excel file, is there any way to remove the top line that says
DataTables - JS Bin
See this example.
Kevin
Thanks again Kevin for the quick reply.
Latest test case at http://live.datatables.net/lulopoku/22. I made the update to the CSV and TSV files that you made to the Excel.
This is now not displaying the headers on the Excel, CSV and TSV files. However, if I click the options button again and check the checkbox to display the headers, the headers do not show up in the export.
Sorry, you need to use a function to return the updated
displayHeaders
value. Otherwise it is set to the current value during initialization and won't change. Updated example:http://live.datatables.net/nizukiqa/1/edit
Kevin
I tried the updated example at http://live.datatables.net/nizukiqa/1/edit, but it still doesn't work for me on the Excel file. It always displays the headers even if I check the box not to.
Guess I should pay more attention I guess this in one of the cases where Datatbles won't take a function as the option value.
Its possible to create a function, using
customize
, to remove the header if needed. Seems like a lot of work.Another option would be to create two of each button. One with
header: true
and the other withheader: false
. Hide all the false buttons then if the option is changed hide all theheader: true
buttons and make visible theheader: false
. Here is an example:http://live.datatables.net/nizukiqa/2/edit
Note I changed the name of the
header: false
button to Excle1 just so you can see the change. Likely you will keep them the same so the user doesn't notice anything.Kevin
Thanks Kevin. That worked.