I just recently made a knockout upload request for them to add two events to define in binding, expand, then call the correct points before rendering the elements and after all the elements have been displayed. I have not heard anything from them, but it does exactly what you want to do, but you do not need to write hacker code to make it work. I am surprised that no one has done this before. I used these callbacks that I added to the source to destroy and reinitialize the knockout related jquery datatable. This was the easiest solution. I have seen many online attempts that try and do it differently, but this is the easiest solution.
Pull request: → pr 1856
ko.bindingHandlers.DataTablesForEach = { init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var nodes = Array.prototype.slice.call(element.childNodes, 0); ko.utils.arrayForEach(nodes, function(node) { if (node && node.nodeType !== 1) { node.parentNode.removeChild(node); } }); return ko.bindingHandlers.foreach.init(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext); }, update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { var value = ko.unwrap(valueAccessor()), key = "DataTablesForEach_Initialized"; var newValue = function() { return { data: value.data || value, beforeRenderAll: function(el, index, data) { if (ko.utils.domData.get(element, key)) { $(element).closest('table').DataTable().destroy(); } }, afterRenderAll: function(el, index, data) { $(element).closest('table').DataTable(value.options); } }; }; ko.bindingHandlers.foreach.update(element, newValue, allBindingsAccessor, viewModel, bindingContext);
Knockout Datatables JSFiddle
Zach Painter Jun 05 '15 at 17:41 2015-06-05 17:41
source share