Update cells continuously (general question)
Update cells continuously (general question)
I'm thinking about using DataTables in a project but I'm not sure about the ajax option. Is it possible to use the ajax option if I want to continuously update the table cells with data that I retrieve via ajax (every second) or does the option only allow a single call? Alternatively, I would exclude the ajax call in a seperate function using setInterva() and use the API to update the cells. If anybody has experience with a similar usecase, I'd be very thankful.
This question has an accepted answers - jump to answer
Answers
You can use
ajax.reload()
to fetch the data from the server if using theajax
option. This would be the easiest option if you want to completely reload the table.You can do this then use the API to update the cells. Which API's you use will be determined by how you want to update the cells. See the following docs for some of the API's you can use:
clear()
rows.add()
row().data()
orrows().data()
cell().data()
orcells().data()
Let us know if you have further questions.
Kevin
Thank you for the quick repsonse!
In my case, each row respresents a measurement channel. Depending on whether the row's measurement has been started by the user, cell data will change.
For now, I have implemented a loop which iterates through the newly fetched data and DataTables data. If cell data has changed, I set the data with cell.data() and I use row.invalidate() to visualize the changes. I use row.add().draw() and row.remove().draw() to add/remove rows.
If the number of colums changes, I use destroy(), empty() and initialize the the DataTable again. I'm not sure if that is the best way to handle new columns.
Does that approach seem reasonable all in all?
Sounds reasonable and you need to use
destroy()
andempty()
when changing the number of columns.Kevin
Thanks again!
I have another follow-up question regarding the update. The table seems unresponsive for click events and typing into input fields during an update of cells using the described method.
Do you have any suggestion what could cause the problem?
If someone is interacting with the table when you destroy it then I can see problems occurring. If this is the problem then I would look at using a global variable boolean that is toggled when the user is interacting with the table to stop the destroy process.
Kevin