Update a column by rowId
Update a column by rowId
tisawyer
Posts: 12Questions: 4Answers: 1
in DataTables
Is it possible to update one or more columns in a row given a rowId and the column data? Or is it necessary to delete the entire row then add the row back in with all columns?
This discussion has been closed.
Answers
I'd be happy with updating the entire row as it looks possible with
row().data()
. I'd like to use my#rowId
to update the row. Is that possible?I have the same problem.
when I again call the DataTable he loses the information in
table.row (this) .data ();
I do not know how to get the row identifier after it is destroyed and replaced simply by another ajax call .
In order to figure out which row to update I'm doing the below function below. But after a number of calls to this function console.log() starts going crazy and the browser locks. Is it not ok to paw though a DataTable this way?
If you know both the column and the row, then you can address the specific cell using
cell().data()
. Thecell()
selector has the option of being passed as a combination of a row and column selector to get the required cell.Allan
Problem was that things were blowing up when searching for the rowId to update. For now I find jquery alone suits my needs. Thanks for all the help.
I feel you are going about this all wrong. Also, updating the html DOM element will not be reflected in DataTables cached cell data.
As Allan said, if you know the exact row, which it appears you do, then you can use:
However, what this does is effectively wipe all <td> elements in the row and recreates them.
Another solution I have found is to use the following in conjunction:
https://datatables.net/reference/api/rows().invalidate()
https://datatables.net/reference/api/cells().render()
This will update only the contents of each <td> by calling each columns render function.
Other than updating the DOM, what is 'all wrong'? Is there a better way, perhaps without updating the DOM and then
invalidate
?The beauty of DataTables is that it holds a pointer back to the original data used to populate it, if it still exists.
So lets say I update some of the values in the array/object used to populate the first row in my DataTable. All I simply need to do next for it to reflect in my DataTable is the following:
Now, not only will the DOM be updated, but the cached data used for sorting and searching will be updated.
Here's a handy function I built that will bulk add or update / add rows dynamically. Your data will need a unique identifier defined using the "rowId" option on your table config.
Thanks for sharing that with us!
Allan