Export Filter Table Button Serverside

Export Filter Table Button Serverside

gwiqugwiqu Posts: 8Questions: 4Answers: 1

How would i go about creating a button that will call the server to send the entire filtered table to the user as an excel file?

i tried using a custom button to call the specific url for downloading but even though the server sends back a file, it doesnt get downloaded to my computer? im using Flask return Response

Answers

  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited August 2022

    Why would you call the server? If it is just about the table you can use the built-in Excel export buttons as well without having the need to call the server.

    But anyway: Here is something that creates a downloadable file on the server and downloads it to the client on button click.

    I have a button that downloads a lot of documents in a zip file to the client. This line does the actual downloading:

    window.location.href = data.zip;
    

    "data.zip" being the web-path of the file to be downloaded (in my case a .zip file and in your case .xlsx).

    That is basically it. I save the created file on the server so that it can be downloaded. Afterwards I delete this temporary file because I don't want to keep it on the server. The timeout I use is more or less arbitrary - but there is no hurry to get the temporary file deleted.

    //custom button to download all ctr management documents of one selected installation
    $.fn.dataTable.ext.buttons.downloadInstDocs = {
        //only enabled when one row is selected (like edit / delete)
        extend: 'selectedSingle', //alternative would be 'selected' (multiple rows)
        name: "downloadInstDocsButton",
        text: downloadInstDocsLabel,
        action: function ( e, dt, button, config ) {
            var selected = dt.row( {selected: true} );
            if (selected.any()) {
                $.busyLoadFull("show");
                $.ajax({
                    type: "POST",
                    url: 'actions.php?action=zipInstDocuments',
                    data: {
                        ctrInstId: selected.data().ctr_installation.id
                    },
                    dataType: "json",
                    success: function (data) {     
                        window.location.href = data.zip;
                        $.busyLoadFull("hide");
                        setTimeout(function () {
                            $.ajax({
                                type: "POST",
                                url: 'actions.php?action=deleteZipInstTmpFile',
                                data: {
                                    deleteWebPath: data.zip
                                }
                            });
                        }, 10000);
                    }
                });
            }
        }
    };
    
    buttons: [
        {   extend: "downloadInstDocs", className: "SubscriptionOnly" }
    ]
    
  • rf1234rf1234 Posts: 3,000Questions: 87Answers: 421
    edited August 2022

    Here is more on how to send an entire data table to the server:
    https://datatables.net/forums/discussion/comment/168298/#Comment_168298

Sign In or Register to comment.