Customizing the data from export buttons

Customizing the data from export buttons

sulemanjohnsulemanjohn Posts: 3Questions: 1Answers: 0

Hi All,

After reading some blogs and articles I implemented this logic to export the serverside data. I was successful to print all the data I receive from external source. But the problem is that I have 4 JQuery DataTables on one page and "jQuery.fn.DataTable.Api.register('buttons.exportData()'" binds this event with all the Datatables. Is there any way I can make it available to one datatable

    jQuery.fn.DataTable.Api.register('buttons.exportData()', function (options) {
        if (this.context.length) {
            var exportHeader = $("#example thead tr th").map(function () { return this.innerHTML; }).get();
            var exportBody = GetDataToExport();
            return { body: exportBody, header: exportHeader };
        }
    });

function GetDataToExport() {
         var jsonResult = $.ajax({
                    url: 'myServerSide.json?page=all',
                    data: {search: $("#search").val()},
                    success: function (result) {},
                    async: false
                });
        var exportBody = jsonResult.responseJSON.data;
        return exportBody.map(function (el) {
            return Object.keys(el).map(function (key) { return el[key] });
        });
    }

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin
    Answer ✓

    To check my understanding of what you are looking for here - you want to override buttons.exportData() for one specific table? Interesting idea, I don't think I've thought of that as an option before, and I don't think it would be easily done with the current API.

    What I'd suggest instead is to make use of the customizeData callback for the buttons.exportData() method. That way you can get the data from the server in the customizeData callback and then mutate the data (note that you must mutate it, not return, since Buttons doesn't do anything with the return).

    So you might have:

    {
      extend: 'print',
      exportOptions: {
        customizeData: function (d) {
          var exportBody = GetDataToExport();
          d.body.length = 0;
          d.body.push.apply(d.body, exportBody);
        }
      }
    }
    

    Its worth pointing out that you are getting all of the data from the server-side to the client-side for this, so is utalising server-side processing worth it at all? If the majority of your users are going to do a print, I'd suggest not - just download the whole data set up front.

    Allan

  • sulemanjohnsulemanjohn Posts: 3Questions: 1Answers: 0

    I appreciate your effort and support. This is what I want. Serverside option is set to true for pagination. I need to save the checked rows somewhere in local array variable. GetDataToExport() will return data from that variable instead of calling the server. But my issue was That I had 4 Datatables on one page with print buttons and jQuery.fn.DataTable.Api.register('buttons.exportData()' was binding with all datatables.

    Now I will not use jQuery.fn.DataTable.Api.register('buttons.exportData()' anymore and use the customizeData for each Datatable.

    Thanks @allan

This discussion has been closed.