Jquery DataTable invalidate() doesn't work for row that have been added dynamically
Jquery DataTable invalidate() doesn't work for row that have been added dynamically
Hello, I'm using the JQuery plugin DataTable, I have a button which allows to the user to add an empty row. Also, all my cells are editable when you double-click on (I made my own function).
When the user double click on a cell, he can edit the cell, and to save the content, the user has to click out the cell. To do that, I call a function when the cell lose the focus that call the .invalidate() method of datatable see here (invalidate function) to tell to datatable to refresh the cache with the DOM, so the filter will be able to find the new data added in the table.
But for dynamically added rows that doesn't work, in fact calling the .invalidate() function causes an issue, the content of the cell is cleared.
Here my codepen.io :http://codepen.io/robert91/pen/wWwmeE
Thank you for your help !
This question has an accepted answers - jump to answer
Answers
That is correct.
row().invalidate()
requires a row selector, therefore it only works on existing rows. This is perhaps too implicit in the documentation and needs to be explicit.You would use
row.add()
orrows.add()
to add a row to a DataTable.Allan
Thanks for your reply,
I don't understand because when I click on my 'add button' I do the code below :
var row = myTable.row.add(emptyArrayOfElement).draw(false).node();
myTable.page('last').draw('page');
So, I added a row to my DataTable and the row is existing no ?
Ah - sorry. I slightly misunderstood. The issue is that you use
row.add()
to add the data from an array, so when you callrow().invalidate()
on it attempts to get the new data from the original data source (i.e. the array).But your contenteditable updates the DOM, not the array. So you need to tell DataTables to read from the DOM. You do that by passing
dom
into thecell().invalidate()
method: http://codepen.io/anon/pen/yJLZjv .Allan
Thaaaank you aaaa loooooooooooot !!!!!!I searched the solution for several days !!
I don't know how works your forum and I know it's not in this post I have to say it , but I want to congratulate all the developers of this free plugin !!
Thank you :-). Glad I could help.
Allan