facing problem fitting api data in customizeData function in exportOptions

facing problem fitting api data in customizeData function in exportOptions

sakib_shahriarsakib_shahriar Posts: 2Questions: 1Answers: 0
edited January 29 in Free community support

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

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin

    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

  • sakib_shahriarsakib_shahriar Posts: 2Questions: 1Answers: 0

    @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.

  • allanallan Posts: 63,434Questions: 1Answers: 10,458 Site admin

    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

  • kthorngrenkthorngren Posts: 21,289Questions: 26Answers: 4,943

    This button plugin might be useful if you choose to export the file server-side.

    Kevin

Sign In or Register to comment.