How to copy / export, ... all lines in DataTables

How to copy / export, ... all lines in DataTables

sloloslolo Posts: 127Questions: 27Answers: 2

Link to test case: https://live.datatables.net/patumuhi/1/edit
Debugger code (debug.datatables.net): NA
Error messages shown: NA
Description of problem: Hello,

It is probably a stupid question but I am not able to do the following things below:
1./ How to copy, export, ... all lines from a DataTable if no lines are selected (even if I see 10 lines on 50)

Yet in this example it works just fine without doing anything special:
https://datatables.net/extensions/buttons/examples/html5/simple.html

2./ How to avoid to select a line when clicking on the button "Action" (see DataTable without checkbox)

Othewise, if you select all lines (with checkbox in header) and unselect all, there is a small issue in html (see screenshot)

can you confirm this behavior.

thanks in advance for your help and have a nice day.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,339Questions: 1Answers: 10,838 Site admin

    1./ How to copy, export, ... all lines from a DataTable if no lines are selected (even if I see 10 lines on 50)

    Do you have server-side processing enable (serverSide)? If so, please see this FAQ. If not, please link to a test case showing the issue.

    2./ How to avoid to select a line when clicking on the button "Action" (see DataTable without checkbox)

    Use select.selector to limit which columns (or items) the selection handler is applied to. See this example for how it might be done on the first column only.

    Othewise, if you select all lines (with checkbox in header) and unselect all, there is a small issue in html (see screenshot)

    That's just how the browser is displaying the document in your inspector. input is a short tag and doesn't need a closing tag in HTML5.

    Allan

  • sloloslolo Posts: 127Questions: 27Answers: 2

    Hi @allan,

    Like always when is ask for a question the test case is in the header of the post ;)

    Do you have server-side processing enable (serverSide)? If so, please see this FAQ. If not, please link to a test case showing the issue.

    No, the table is populated once and then before being instantiated by the DataTable.

    Use select.selector to limit which columns (or items) the selection handler is applied to. See this example for how it might be done on the first column only.

    I already use it if you have a look to the test case ;)

  • allanallan Posts: 65,339Questions: 1Answers: 10,838 Site admin

    Hi,

    I did look at the test case, but it doesn't run with a syntax error. I wasn't sure if it was an example code dump that wasn't complete, or something else as a result. Apologies.

    I don't see any specific reason why the export wouldn't include all rows, but if you are able to link to a running test case, I might spot something.

    Also, I see "select" : true, but not select.selector anywhere?

    Allan

  • sloloslolo Posts: 127Questions: 27Answers: 2

    Hi @allan,

    My apologies for the test case. I must have made a mistake setting it up because it wasn't even the correct one I linked.

    The correct test case is this one:
    https://live.datatables.net/qowijewa/1/edit

  • allanallan Posts: 65,339Questions: 1Answers: 10,838 Site admin

    Thanks!

    Export rows: You have "rows": ":visible" so it is only exporting the 10 rows that are visible. If you want it to export all rows, you need to remove that.

    Select: Try selector: 'td:first-child, td:nth-child(2)'. There are other selectors as well that you could use if you need it to be a bit more generic. A good one might be to add a class to the button cell not-selectable (for example) and then use the selector td:not(.not-selectable).

    Allan

  • sloloslolo Posts: 127Questions: 27Answers: 2

    Export rows: You have "rows": ":visible" so it is only exporting the 10 rows that are visible. If you want it to export all rows, you need to remove that.

    This works fine, thanks for reporting this issue.

    A good one might be to add a class to the button cell not-selectable (for example) and then use the selector td:not(.not-selectable).

    This works fine too :)

    Since I've been able to export all lines (even the non-visible ones), I've encountered a new problem.

    I have modified the test case in order to show you what I am talking about:
    https://live.datatables.net/lahozaki/1/edit

    I intentionally modified rows 1 and 11 of the DataTable by adding HTML with line breaks and indentation in the code.

    The problem is here:

          "exportOptions": {
            "format": {
              body: function ( data, row, column, node ) {
                //return node.innerText;
                return data;
              }
            },
    

    As you can if you "copy" all lines into clipboard, the first 10 line are correctly exported if I use return node.innerText; but the 11th line (not visible) keeps its html when exported.

    And if I use return data; instead of return node.innerText; then all lines keep the HTML when exported.

    Is there a good way to export data as they are displayed?

    Do I need to define a type for each column or maybe an orthogonal data value in order to be exported correctly?

  • rf1234rf1234 Posts: 3,188Questions: 92Answers: 438
    edited October 23

    You can do anything with export options. Here is an excerpt from one of mine. It is from an Excel export where more manipulations are required to make it work. Among other things I am also stripping html tags.

  • sloloslolo Posts: 127Questions: 27Answers: 2

    Thanks @rf1234,

    This gives a good overview of what is possible with formatting before exporting values.

    But I admit that I would prefer not to have to "tinker" with the data knowing that it works very well in the case of line 1. In fact, ALL the lines should undergo the same treatment whether they are visible or not.

  • rf1234rf1234 Posts: 3,188Questions: 92Answers: 438

    I think you will have to "tinker" with the data at some point - like it or not. For example the same data may require different formatting when you export them as an excel file or as a csv file.

    While Excel expects "American" number-formatting of the exported values, the csv exporting (partly) requires formatting according to the regional settings of the person making the export. There's tons of threads on this in this forum. Good luck.

  • sloloslolo Posts: 127Questions: 27Answers: 2

    @rf1234,

    You are right but I am pretty sure that DataTables component have a internal method to clean data (probably the same that is used to display the content into cells).

    @allan,

    Can you confirm the behavior I explain in the last test case (that only visible lines are correctly formatted when exported).

    But sure to have return node.innerText; and not return data; in the test case:

          "exportOptions": {
            "format": {
              body: function ( data, row, column, node ) {
                return node.innerText;
                //return data;
              }
            },
    

    see: https://live.datatables.net/lahozaki/3/edit

  • allanallan Posts: 65,339Questions: 1Answers: 10,838 Site admin
    Answer ✓
    return node.innerText.trim();
    

    will remove the whitespace that is part of the text node that you are returning. Given that the text node has white space, and you are returning the text node contents, then yes, I'd say that what it is currently exporting is expected.

    Allan

Sign In or Register to comment.