CSV export with Server-Side

CSV export with Server-Side

culterculter Posts: 102Questions: 24Answers: 0

Hi, I have dataTables table with several customised filters and server-side processing. Everything works fine. Now I need to export csv (for a start). I added

        dom: 'Bfrtip',
        "buttons": [
        {
        text:       'Export CSV',
        action: function (e, dt, node, config) {
            $.ajax({
                "url": "scripts/server_processing.php",
                "data": dt.ajax.params(),
                "success": function(res, status, xhr) {
                    var csvData = new Blob([res], {type: 'text/csv;charset=utf-8;'});
                    var csvURL = window.URL.createObjectURL(csvData);
                    var tempLink = document.createElement('a');
                    tempLink.href = csvURL;
                    tempLink.setAttribute('download', 'export.csv');
                    tempLink.click();
                }
            });
        }
        },

The button 'Export CSV' is visible and the export is working, but only on the visible data on one page. The data are filtered according to selected filter, which is great. So I think the only problem is with pagination. I found several solutions. Many of them manages it by selecting "All" entries and then export, which is no the right way, because I will need to export thousands of rows. Definitely, I need to use a server-side process to create the files to download.

Similar question is here
https://datatables.net/forums/discussion/39680/is-there-a-way-to-export-all-while-using-server-side-processing/p1

but it also exports the visible data only when I implemented it. :(

I have few questions:

To export CSV, do I need special script where will be the data send or I can use the customised server_processing.php which is downloadable from datatables.net website? Is it the same with the export to EXCEL and PDF ?

Is there some easy way how to include all pages to export?

Thank you

Answers

  • kthorngrenkthorngren Posts: 20,307Questions: 26Answers: 4,769

    Please see this FAQ regarding the export buttons and server side processing. The recommended option is to use server based tools for exporting the data to alleviate the need to send all the data to the client.

    Kevin

  • culterculter Posts: 102Questions: 24Answers: 0

    Hi Kevin, thank you, I mentioned it in the question, that I need server-side tools to export data. I just figured it out with modifying the ssp.class.php script where I changed the $limit to export more rows than visible.

    Now I'm going to make export to pdf and xls, hope it will go as easy as csv ;)

    Thanks

  • kthorngrenkthorngren Posts: 20,307Questions: 26Answers: 4,769

    Oh, sorry. Mis-read the question I thought you were still wanting to use the Datatables export tools.

    Kevin

  • geoffsarigeoffsari Posts: 1Questions: 0Answers: 0
    edited July 2019

    @culter, great to hear that you managed to find a way to get the server-side export working for csv files. Are you able to share your script of ssp.class.php to understand how achieved that and so that others can adapt in their project. Thanks

  • francispiresfrancispires Posts: 3Questions: 0Answers: 0

    I have a .net core Generic DataTable Wrapper and i've managed the server side export with a simple taks.
    Programatically I've set the page to 0 and the length to a big number, attach a callback to draw and in this call back I execute the button funcition. Remove this callback and reload the data table to the page and length that is setted before the user clicks on button.
    this is my buttons element:

    buttons: [
                    {
                        extend: 'print',
                        text: 'Print',
                        className: 'btn btn-primary btn-sm',
                        action: serverSideButtonAction
                    },
                    {
                        extend: 'copy',
                        text: 'Copy',
                        className: 'btn btn-success btn-sm',
                        action: serverSideButtonAction
                    },
                    {
                        extend: 'excel',
                        text: 'Excel',
                        className: 'btn btn-warning btn-sm',
                        action: serverSideButtonAction
                    },
                    {
                        extend: 'pdf',
                        text: 'pdf',
                        className: 'btn btn-danger btn-sm',
                        action: serverSideButtonAction
                    }
                ]
    

    And this is serverSideButtonAction

    function serverSideButtonAction(e, dt, node, config) {
    
            var me = this;
            var button = config.text.toLowerCase();
            if (typeof $.fn.dataTable.ext.buttons[button] === "function") {
                button = $.fn.dataTable.ext.buttons[button]();
            }
            var len = dt.page.len();
            var start = dt.page();
            dt.page(0);
    
            // Assim que ela acabar de desenhar todas as linhas eu executo a função do botão.
            // ssb de serversidebutton
            dt.context[0].aoDrawCallback.push({
                "sName": "ssb",
                "fn": function () {
                    $.fn.dataTable.ext.buttons[button].action.call(me, e, dt, node, config);
                    dt.context[0].aoDrawCallback = dt.context[0].aoDrawCallback.filter(function (e) { return e.sName !== "ssb" });
                }
            });
            dt.page.len(999999999).draw();
            setTimeout(function () {
                dt.page(start);
                dt.page.len(len).draw();
            }, 500);
        }
    
  • nexeonnexeon Posts: 1Questions: 0Answers: 0
    edited May 2020

    its not working in Chrome anymore :(

    -- I think its about Timeout number vs load speed....

  • colincolin Posts: 15,144Questions: 1Answers: 2,586

    @nexeon We're happy to take a look, but as per the forum rules, please link to a test case - a test case that replicates the issue will ensure you'll get a quick and accurate response. Information on how to create a test case (if you aren't able to link to the page you are working on) is available here.

    Cheers,

    Colin

This discussion has been closed.