Hello, is there a way for KT datatable to call remote data source api with request header, eg. if the api request must include token header? I already check in the documentation for datatable in metronic 9, but the example only covers api call without request header. Also, for the json response format, should I follow the format of example that includes page, pageCount, etc, or can I use only data needed for datatable?
Thank you.
Hi
Yes, for server-side pagination to work correctly with KTDataTable, the API response needs to include certain metadata about the total number of records. Based on the code, here's what your API response should look like:
{
"data": [
// Your data array here
{ "id": 1, "name": "Example 1" },
{ "id": 2, "name": "Example 2" }
// ...
],
"totalCount": 100 // Total number of records (across all pages)
}
const dataTableOptions = {
apiEndpoint: "https://your-api-endpoint.com/data",
requestHeaders: {
"Authorization": "Bearer your-auth-token-here"
},
mapResponse: function(response) {
return {
// Transform your API response to match the expected format
data: response.items, // or whatever your data array is called
totalCount: response.total // or whatever your total count field is called
};
},
// other options...
};
Hi Abudzar Ali
Sorry for the delay. The KTDataTable component accepts a requestHeaders configuration option that lets you specify custom headers for the API requests:
const dataTableOptions = {
apiEndpoint: "https://your-api-endpoint.com/data",
requestHeaders: {
"Content-Type": "application/json", // Override the default content type if needed
"Authorization": "Bearer your-auth-token-here",
"X-Custom-Header": "CustomValue"
},
pageSize: 10,
// other options...
};
const dataTable = new KTDataTable(element, dataTableOptions);
Thank you and sorry for the late response. I already succeeded in implementing requestHeaders for KTDatatable configuration and shown the data, but the pagination is not working because it shows "1-NaN of undefined" on pagination part. Should i also include the number of data (count) in the json response for the api url, or maybe there are some mandatory response value that needed for KTDatatable api url, especially in server side pagination case?