buttons.exportData using array result
buttons.exportData using array result
kjdion84
Posts: 9Questions: 3Answers: 0
I'm trying to get my table to export using the result of an AJAX request to my server.
Here is the code I am using:
jQuery.fn.DataTable.Api.register( 'buttons.exportData()', function ( options ) {
if ( this.context.length ) {
var jsonResult = $.ajax({
url: 'http://leadbind.dev/backend/leads/datatable',
//data: {search: $('#search').val()},
success: function (result) {
//Do nothing
},
async: false
});
console.log(jsonResult.responseJSON.data);
return {body: jsonResult.responseJSON.data, header: $("#leads_datatable thead tr th").map(function() { return this.innerHTML; }).get()};
}
} );
When I click the export button, I see this result for the request:
{draw: 0, recordsTotal: 9, recordsFiltered: 9,…}
data
:
[{id: "1", segment_id: "1", source_id: "1", source_cost: "55.00",…},…]
draw
:
0
input
:
[]
queries
:
[,…]
recordsFiltered
:
9
recordsTotal
:
9
The console.log result for jsonResult.responseJSON.data is:
Array(9)
0
:
{id: "1", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
1
:
{id: "2", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
2
:
{id: "3", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
3
:
{id: "4", segment_id: "1", source_id: "2", source_cost: "0.00", data: {…}, …}
4
:
{id: "5", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
5
:
{id: "6", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
6
:
{id: "7", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
7
:
{id: "8", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
8
:
{id: "9", segment_id: "1", source_id: "1", source_cost: "55.00", data: {…}, …}
length
:
9
__proto__
:
Array(0)
It creates the excel file but there are no rows in it, only headings. How do I fix this?
This discussion has been closed.
Answers
It looks like you are replacing Buttons' built in
buttons.exportData()
. Its a clever idea to have it do what you are looking for, but you need to have your custom implementation return the same JSON data structure as the built inbuttons.exportData()
. That is the structure that anything is using that method will be expecting.Allan
https://datatables.net/forums/discussion/49133
and look for @SGConger Comments at the bottom of the page