What should you do when only the body of the table is updated?
What should you do when only the body of the table is updated?
vladi
Posts: 18Questions: 4Answers: 0
The body of the table (tbody) is updated, and after updating the tbody, the filters are not updated and the row sorter stops working.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
What should i do when only the body of the table is updated?
See this FAQ about how to tell Datatables about changes to the DOM (
tbody
).Kevin
I have some problems.
1) I delete all the rows from tbody
var friendDataTable = $('#friendTable').DataTable();
friendDataTable.rows().remove().draw();
2) then destroy the DataTable object
friendDataTable.destroy();
3) load new rows with data (table rows are filled in on the server and come already in the form of ready-made html markup)
// Update tbody new rows with data
$('#TbodyFriendTable').replaceWith(data);
4) reinitialize the DataTable
I have 2 filters in each column of the table - the old and the new filters
How reinitialize the DataTable:
function UpdateTablePlagin () {
};
What you have seems like it should work. Without seeing the code running its hard to say what the problem might be.
Have you looked at the browser's console for errors?
Can you post a link to your page or a test case replicating the issue so we can help debug?
https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case
Kevin
There are no errors.
It does not remove old filters and adds new ones
This is full code:
http://live.datatables.net/cuxeyuja/2/edit
I updated your test case with sample data and a button click event to use your
success
function code. The result of callingUpdateTablePlagin()
is that the updated select option input is appended to the previous. The list is correct but now you have two. You will need to remove the previous list first assuming this is the same problem you are seeing.http://live.datatables.net/cuxeyuja/3/edit
If you have a different issue then please update the test case to show the issue.
Kevin
Yes Kevin, that's the problem. Old filters and sorters are not removed.
How can I remove old filters and sorters?
I would suggest modifying your code a little, so that it would check to see if there is a
select
element already in the column header. If there is, then empty and use it. If not, then create one and append it to the header.You'll likely also want to remove the event handlers you add to the
select
elements on each recreate - so the old ones don't get triggered again! That would be a sure fire path to madness.Finally, I'm not sure you need to actually destroy and create the table each time. You are just changing the data in the table, not the columns or options. So you could use
rows().remove()
androws.add()
and then regenerate your options based on that, which would be much more efficient.Allan
Could you show in this example how you see it? live.datatables.net/cuxeyuja/4/edit
Developing the code for this would fall under out support packages.
Allan
Does this require changes to the Databloss library?
No.
You are already using
rows().remove()
in yoursuccess
function. Replace thefriendDataTable.destroy();
withrows.add()
, something like this:Note the use of a jQuery object to add the rows. Note the
rows.add()
docs state this:You may need to modify thee returned HTML data, ie, don't return the
tbody
tag. You will then need to move theinitComplete
code into a function and call it to update the select inputs.Kevin
Thank you very much, Kevin! I will try it