Hi,
I have a question about Refresh/Reinitialize DataTable.
Im using Metronic v9.0.4 with React. The DataTable component is working fine and now I want to implement DataTable Refresh function. How to refresh/reinitialize the DataTable when a button was clicked without reload the page?
Hi CK Chang,
Apologies for the delay. Could you try calling the `getInstance` function from the existing DataTable instance like this?
const datatable = new KTDataTable(element, dataTableOptions);
datatable = KTDataTable.getInstance(datatableEl);
Hi CK Chang,
Make sure the reload function is called after the DataTable component has been created and mounted in the DOM. You could achieve this by:
Placing the button within the component that renders the DataTable, and only enabling it after the DataTable has been successfully initialized.
Or using a state variable to track the DataTable's initialization status and conditionally render the button
import { useEffect, useState } from "react";
function MyComponent() {
const [dataTableInitialized, setDataTableInitialized] = useState(false);
useEffect(() => {
// Initialize your DataTable here
// ...
// After successful initialization
setDataTableInitialized(true);
}, []);
const handleRefresh = () => {
// ... your existing handleRefresh logic
};
return (
<div>
{/* ... your DataTable */}
{dataTableInitialized && (
<button onClick={handleRefresh}>Refresh</button>
)}
</div>
);
}
Thanks for sharing the code. I tried to follow the code structure that you provided. However the result is still the same.
I can retrieve the datatable instance when I initialize the datatable. But when I trigger the handleRefresh()
function, the datatable element is successfully retrieved, while the datatable instance is not. What I retrieve in this line of code: const datatable = KTDataTable.getInstance(datatableEl);
in console log is KTDataTable with null uid, element and such
import { useEffect, useState } from "react";
import { KTDataTable, KTDataTableConfigInterface } from "../metronic/core/components/datatable";
function MyComponent() {
const [dataTableInitialized, setDataTableInitialized] = useState(false);
useEffect(() => {
// DataTable initialization
const apiUrl = "apiUrl";
const element = document.querySelector("#product_cat_data_table") as HTMLElement;
const token = "token";
const dataTableOptions: KTDataTableConfigInterface = {
apiEndpoint: apiUrl,
requestMethod: "GET",
requestHeaders: {
"Authorization": `Bearer ${token}`,
},
pageSize: 5,
stateSave: false,
columns: {
//...
},
};
const datatable = new KTDataTable(element, dataTableOptions);
setDataTableInitialized(true);
}, []);
const handleRefresh = () => {
// Handle Refresh
const datatableEl = document.querySelector("#product_cat_data_table") as HTMLElement;
const datatable = KTDataTable.getInstance(datatableEl);
datatable.reload();
};
return (
<>
<div data-datatable="true" `id`="product_cat_data_table">
</div>
{dataTableInitialized && (
<button onClick={handleRefresh}>Refresh</button>
)}
</>
);
}
export default MyComponent;
You can use the `reload` function to refresh the DataTable without reloading the page. Here’s an example:
KTDataTable.getInstance(yourDataTableElement).reload();
This will reinitialize the DataTable and fetch fresh data when the button is clicked.
Thanks for your reply. I tried the method you mentioned but it doesn't seem to achieve my goal. This is the scenario:
Assume my DataTable for id 'product_cat_data_table' was successfully created.
When this button clicked:
<button className="btn btn-sm btn-primary text-white" onClick={handleRefresh}>Refresh</button>
const handleRefresh = async () => {
const datatableEl = document.querySelector("#product_cat_data_table") as HTMLElement;
KTDataTable.getInstance(datatableEl).reload();
}