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

apfanzapfanz Posts: 11Questions: 3Answers: 0

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

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    edited January 2021

    Use your variable displayHeaders as the value for the headers 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

  • apfanzapfanz Posts: 11Questions: 3Answers: 0

    Thanks for the quick reply. In the Excel file, is there any way to remove the top line that says

    DataTables - JS Bin

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    See this example.

    Kevin

  • apfanzapfanz Posts: 11Questions: 3Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736

    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

  • apfanzapfanz Posts: 11Questions: 3Answers: 0

    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.

  • kthorngrenkthorngren Posts: 20,142Questions: 26Answers: 4,736
    Answer ✓

    Guess I should pay more attention :smile: 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 with header: false. Hide all the false buttons then if the option is changed hide all the header: true buttons and make visible the header: 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

  • apfanzapfanz Posts: 11Questions: 3Answers: 0

    Thanks Kevin. That worked.

This discussion has been closed.