Download all rows when using server-side processing

Download all rows when using server-side processing

sbarnettsbarnett Posts: 23Questions: 5Answers: 0

I am aware that there is a section in the selection-modifier documentation which explains that this setting has no effect when using server-side processing as the relevant rows are only returned from the server.

However, is there any way I can add a button to "download all rows", regardless of how many (or how few) are displayed on the client at the time?

I realise that this process would take a long time when there are a lot of rows to download - but the same is true when the user selects "show all rows" before they click the Download button and in most cases, my users just want to download them all, regardless.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    You can use the lengthMenu option and add a -1 option to the length. That means "show all" to DataTables (obviously the server-side processing script would also need to accept that and not perform an OFFSET / LIMIT).

    Allan

  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0

    Ah yes, but what I'm trying to avoid is the user having to wait for the server to return all the rows before they click the download button.

    Or is that not possible?

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    You could have a custom button that would sent the page length to -1 (page.len()), load all rows and then trigger the download action. Once that it complete it could then set the page length back to what it was .

    Allan

  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0

    I just noticed that, in your FAQ, you recommend doing this kind of thing by getting the custom button to call some kind of server side process to generate the file.

    Would you say that would make more sense in this case in comparison to the option mentioned above?

    If not, I'll go the above route - I'm just not entirely sure how to trigger the download action from the button function.

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin
    Answer ✓

    Generally speaking yes, I would recommend the approach suggested in the FAQ. The key reason for that is that you won't hit performance limitations in the client's browser. Isn't not bad for a CSV file, but for Excel and in particular PDF, creating a large file on the client-side can be really slow.

    What I would suggest is a custom button be used which basically sets the window.location.href to wherever you have a script that will create the desired output. You can set the HTTP headers with that script to force a browser download.

    Allan

  • sbarnettsbarnett Posts: 23Questions: 5Answers: 0

    OK, in that case, I think it might make sense to go with the proper way of creating the file on the server in my case.

    Thanks.

This discussion has been closed.