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

khoya91khoya91 Posts: 6Questions: 2Answers: 0

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

  • allanallan Posts: 63,799Questions: 1Answers: 10,514 Site admin

    But for dynamically added rows that doesn't work

    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() or rows.add() to add a row to a DataTable.

    Allan

  • khoya91khoya91 Posts: 6Questions: 2Answers: 0
    edited May 2016

    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 ?

  • allanallan Posts: 63,799Questions: 1Answers: 10,514 Site admin
    Answer ✓

    Ah - sorry. I slightly misunderstood. The issue is that you use row.add() to add the data from an array, so when you call row().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 the cell().invalidate() method: http://codepen.io/anon/pen/yJLZjv .

    Allan

  • khoya91khoya91 Posts: 6Questions: 2Answers: 0

    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 !!

  • allanallan Posts: 63,799Questions: 1Answers: 10,514 Site admin

    Thank you :-). Glad I could help.

    Allan

This discussion has been closed.