Update cells continuously (general question)

Update cells continuously (general question)

schokratesschokrates Posts: 4Questions: 2Answers: 0

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

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    Answer ✓

    continuously update the table cells with data that I retrieve via ajax (every second)

    You can use ajax.reload() to fetch the data from the server if using the ajax option. This would be the easiest option if you want to completely reload the table.

    Alternatively, I would exclude the ajax call in a seperate function using setInterva() and use the API to update the cells.

    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:

    Let us know if you have further questions.

    Kevin

  • schokratesschokrates Posts: 4Questions: 2Answers: 0

    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?

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    Sounds reasonable and you need to use destroy() and empty() when changing the number of columns.

    Kevin

  • schokratesschokrates Posts: 4Questions: 2Answers: 0

    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?

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923

    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

This discussion has been closed.