facing problem fitting api data in customizeData function in exportOptions
facing problem fitting api data in customizeData function in exportOptions
sakib_shahriar
Posts: 2Questions: 1Answers: 0
I am using datatable with serverSide processing in angular 16 application. to export all data at once, i tried to set the data in customizeData callback in exportOptions. Problem is when ever i use the await keyword, it doesn't wait for the data to arrive, datatable exports the the before the response come.
public async callGetAsync(url: string) {
const getUrl: string = this.baseUrl + url;
return this.httpClient.get(getUrl).toPromise();
}**
buttons: [
{
extend: 'excel',
text: 'Excel',
exportOptions: {
customizeData: async (data : any) => {
const response: any = await this.httpService.callGetAsync(`branch-location-mapping`);
const apiData = response.data.map((obj: any) => Object.values(obj));
data.body.length = 0;
data.body.push.apply(data.body, apiData);
}
}
}
Answers
That is expected. The
customizeData
method is synchronous. There is nothing in the documentation to suggest that it would wait for a promise, so I'm not sure why you thought it would - did you read it somewhere, or was it just an assumption?It is a good idea though - thank you for the suggestion. I will look into adding that.
Allan
@allan, do you have any workaround for exporting full data from api when I am using server side pagination in datatable? that would be very helpful.
In all honesty, sending the entire data set to the client-side rather defeats the point of server-side processing. Depending on the amount of data in question, you might be best creating the export file on the server-side and then downloading it to the client. If I were using server-side processing, then that is what I would do myself. Otherwise, you'd be as well just loading all of the data up front.
The other option is to modify the Buttons code to accept a Promise in return from
customizeData
and allow that to resolve before it carries on.Allan
This button plugin might be useful if you choose to export the file server-side.
Kevin