DataTables duplicating table rows when drawn
DataTables duplicating table rows when drawn
Zxurian
Posts: 26Questions: 6Answers: 0
so I've got an interesting problem that I've not encountered before.
I'm currently using DataTables with an ajax source, and when datatables does it's fetch of data and displays it to the table, it duplicates every row in the table. Refreshing the page a few times will sometimes cause it to display the content properly, other times, it requires just leaving and coming back later.
The following is my initialization: http://pastebin.com/FU7VBAtN
I'm currently using DataTables with an ajax source, and when datatables does it's fetch of data and displays it to the table, it duplicates every row in the table. Refreshing the page a few times will sometimes cause it to display the content properly, other times, it requires just leaving and coming back later.
The following is my initialization: http://pastebin.com/FU7VBAtN
This discussion has been closed.
Replies
(p.s., the forum view says there was 1 comment prior to posting this, but when I click on it, I only see my original post)
However in answer to your question, there is nothing obvious in your initialisation as to why this would happen. Are you seeing any JS errors on the console or anything? Perhaps you could run the table through the debugger when this occurs ( http://debug.datatables.net ) so I can see if there is anything particularly odd happening, but I'm not sure what it would be to be honest!
One thing I would say, rather than using static events applied in fnDrawCallback - you'd be better off using live events: http://datatables.net/release-datatables/examples/advanced_init/events_live.html
Allan
In regards to using live vs. fnDrawCallback,I looked up .live() on jQuery's website, but it looks like they're depreciating it in favor of .on(), and according to the documentation, it looks like the HTML elements have to be present within the DOM before using it, so if I'm inserting an element into the table via an fnUpdate(), wouldn't I still need to place it inside the fnDrawCallback(), since that fires after the element has been added?
No - you would just use the delegate options of .on. For example
[code]
$('#example tbody').on('click', 'tr', function () {...});
// would work the same as:
$('#example tbody tr').live( 'click', function () {...});
[/code]
They aren't removing functionality, just refactoring the API.
Allan
I refactored the code from inside back to
[code]$(".datatable tbody tr td").on("click", ".invoice", function() {...});[/code]
but it's not firing when I click on the element with a class="invoice" inside the cell
Allan
By using [code]$(".datatable").on("click", ".invoice", function(event){ ... });[/code] it properly binds to newly created items. Just had to be more general with the initial selector.