Data Export Questions

Data Export Questions

trongarttrongart Posts: 222Questions: 51Answers: 0

This example uses a copyHtml5 and a csvHtml5 button: live.datatables.net/tudacode/1/edit

I am struggling to find a solution to the following:

copyHtml5

Is it possible to remove the "DataTables - JS Bin" at the beginning of the copied text (for some reason it says "Exported Data" in my project instead)?

Can we remove all spaces from the copied text?

Is there maybe a way to not add a comma after the last row/copied cell?

csvHtml5

Is it possible to export the data as a .txt file instead of .csv or to rename it before export?

When opening the .csv file in a text editor, all exported cells have quotation marks around them. Is it possible to remove these?

This question has accepted answers - jump to:

Answers

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794

    copyHtml5

    Is it possible to remove the "DataTables - JS Bin" at the beginning of the copied text (for some reason it says "Exported Data" in my project instead)?

    Take a look at this example and the buttons.exportInfo() docs it links to. I believe you will want to set title to null or "".

    Can we remove all spaces from the copied text?

    Not sure if there are any examples on the forum but the customize function is used to modify the exported contents.

    Is there maybe a way to not add a comma after the last row/copied cell?

    I guess the comma is part of your data? Use the customize function.

    csvHtml5

    Is it possible to export the data as a .txt file instead of .csv or to rename it before export?

    See this example for setting the filename.

    When opening the .csv file in a text editor, all exported cells have quotation marks around them. Is it possible to remove these?

    Try setting the fieldSeparator option found in the csvHtml5 options.

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    @kthorngren Thank you so much for this! Most of it worked here:
    live.datatables.net/vatoruli/1/edit

    Still struggling with the following:

    No comma after the last copied cell value: Not sure how to use the customize function on the copied text from copyHtml5. Alternatively, I was thinking to add fieldSeparator: ',' to only have commas between copied cell values but this does not seem to work for copyHtml5.

    Text file export: Changing the filename for csvHtml5 to Test.txt exports the data as Test.txt.csv - how can I remove .csv from the exported file?

  • kthorngrenkthorngren Posts: 20,425Questions: 26Answers: 4,794
    edited October 2021

    Thanks for the test case.

    No comma after the last copied cell value

    Looks like the row parameter of the body function is the row counter. You can compare the row counter to the number of rows being exported, using something like this:

    var lastRow = table.rows({page: 'current'}).count() === row + 1;
    

    Note the use of the selector-modifier of {page: 'current'} to match rows: [':visible'],. This also uses the count() API.

    Test.txt exports the data as Test.txt.csv - how can I remove .csv from the exported file?

    Good question. I tried a function and it still added .csv. @allan or @colin will need to provide input for this issue.

    See the updated example for both questions:
    http://live.datatables.net/padomemo/1/edit

    Kevin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    @kthorngren Thank you for your help! I added another example based on yours to get no comma after the last copied cell for all rows of filtered results (not just visible rows):

    live.datatables.net/molujaye/1/edit

    It works quickly on the test case, but when we have 9000+ rows, this takes a very long time to load. Is there another way to get the rows count for filtered results quicker?

  • colincolin Posts: 15,177Questions: 1Answers: 2,590
    Answer ✓

    I suspect the line

    var lastRow = table.rows({search: 'applied'}).count() === row + 1;
    

    will be inefficient, as this is getting all the rows to just count them. It would be better to use something like:

    var lastRow = table.page.info().recordsDisplay === row + 1;
    

    Could you see if that helps, please,

    Colin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    @colin Yes this is much faster! Thank you very much!

    Do you think there is a way to export the data as a .txt file? No matter what, the export is always in .csv or .txt.csv for csvHtml5.

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    A CSV is just a text file, so you should set the fieldSeparator to ' ' (a space) and that would generate a more standard text file.

    Colin

  • trongarttrongart Posts: 222Questions: 51Answers: 0
    edited October 2021

    @colin Adding the fieldSeparator ' ' exports a "Test.txt .csv" file with a space and the .csv remains:

    live.datatables.net/lojokemi/1/edit

  • colincolin Posts: 15,177Questions: 1Answers: 2,590
    Answer ✓

    Ah, yep, you also need to set the extension:

                        filename: 'Test',
                        extension: '.txt',
    

    See here: http://live.datatables.net/lojokemi/2/edit

    Colin

  • colincolin Posts: 15,177Questions: 1Answers: 2,590

    Please see the full list of options here:

    https://datatables.net/reference/button/csvHtml5

    Colin

  • trongarttrongart Posts: 222Questions: 51Answers: 0

    Thank you!

Sign In or Register to comment.