How to export all rows from Datatables using Ajax?

前端 未结 12 1199
广开言路
广开言路 2020-11-29 23:38

I am using new feature in Datatables: \"HTML5 export buttons\". I am loading data with Ajax.

https://datatables.net/extensions/buttons/examples/html5/simple.html

12条回答
  •  鱼传尺愫
    2020-11-29 23:47

    Just wanted to post an actual answer for people struggling with this.

    If you are exporting using the excel button, you can use the customizeData button property to format the data going to excel a moment before it exports.

    I used this to make a synchronous api call to my server to get the data, return it, massage it, and then let it continue on it's way. Code below.

                               {
                    extend: 'excel',
                    customizeData: function (p)
                    {
                        //get the params for the last datatables ajax call
                        var params = JSON.parse(options.dataTable.ajax.params());
                        //flag to tell the server to ignore paging info and get everything that matches the filter
                        params.export = true;
                        UC.Api(options.api.read.getHook(), params, function (data)
                        {
                            p.body = new Array();
                            $.each(data.data, function (i, d)
                            {
                                var item = [d.serial, UC.FormatDateToLocal(d.meta.Date), d.transmission.title, d.transmission.type, d.transmission.information];
                                p.body.push(item);
                            });
                        }, null, { async: false });
                    }
                },
    

提交回复
热议问题