I hope everyone is doing well. I’ve been working on integrating DataTables into my Wappler project and have encountered some issues that I’d like to discuss and hopefully resolve with your insights.
The Problem:
When I try to initialize DataTables in my application, I am encountering an error which reads:
javascriptCopy code
Uncaught TypeError: Cannot read properties of undefined (reading 'appendChild')
Code Structure:
I have a JavaScript file ( dataTablesMain.js ) where I’ve written a function initDataTable() that takes care of initializing the DataTable. Here’s a snippet:
javascriptCopy code
function initDataTable() {
// ... (initialization code here)
let table = $('.dataTable').DataTable({
// DataTable options
});
}
This function is supposed to be triggered after my data is fully loaded (and after waiting 500ms). I’m using Wappler’s dmx-on:updated to trigger this function:
This error (Uncaught TypeError: Cannot read properties of undefined (reading ‘appendChild’)) could be due to an ID error. It’s important to ensure that the HTML element where you want to apply DataTable exists and is available at the time of initialization. Make sure that the selector $(’.dataTable’) is matching the desired element.
The dmx-on:updated event is generally appropriate for initializing functions after a data update. Try using dmx-on:done; I haven’t tested it this way before, but give it a try to ensure that DataTable is initialized only after the complete page has loaded.
I've checked it out, but the error remains. I need to be sure that DataTable initialize after data is available and sometimes happens that the ServerConnect component loads more than one time when you send GET data to the API endpoint
You can create a function in JavaScript and call it after the component has loaded. You invoke the function from within the component, understand? It might work.
ServerConnect Multiple Loads : Is there a way to prevent dmx-serverconnect from loading multiple times, or to ensure that DataTables initializes only after the final load? (for some reason DataTables triggers and error when loading it Script multiple times, it seems like you can’t change data after loading it once)
Initialization Sequence : Is dmx-on:updated the best event to ensure that DataTables initializes only after the table is fully populated?
Conflict Resolution : Are there known issues or best practices for using DataTables with Wappler’s ServerConnect component?
It’s great, yet I see many (including the post creator crystaltaggart) are having a loading issue. The dmx-on:done or dmx-on:success in the data component seems to be triggering the DataTable script before finishing the data fetching, resulting in a ‘No data available in table’ DataTable error. In fact, jellederijke mentioned it on this comment: How to use DataTables.net
Though I added a wait 1ms component before initializing the destroy_dt and load_dt functions, sometimes it doesn’t work as expected (not every loading time works the same). Do you have any idea @patrick how to solve this problem?
Initialization Sequence : Is dmx-on:success or dmx-on:done the best event to ensure that DataTables initializes only after the table is fully populated?
Conflict Resolution : Are there known issues or best practices for using DataTables with Wappler’s ServerConnect component?