Item removed from Knockout observable array, but not from html table

前端 未结 1 1137
萌比男神i
萌比男神i 2021-01-15 21:48

I have a plunk that demonstrates my issue: http://plnkr.co/edit/PzBrcTX0Vnn01xWy4dk6

This is a table that has a list of \"settings\". It uses Footable so the list c

1条回答
  •  春和景丽
    2021-01-15 22:18

    The issue comes from the empty text nodes that surround each element. The foreach binding also tracks those text nodes. See https://github.com/knockout/knockout/pull/709 for a discussion about why it's not possible to just ignore them as a general rule. On the other hand, your custom binding can strip them out.

    See how knockout-sortable does this (for similar reasons):

    var nodes = Array.prototype.slice.call(element.childNodes, 0);
    ko.utils.arrayForEach(nodes, function(node) {
        if (node && node.nodeType !== 1) {
            node.parentNode.removeChild(node);
        }
    });
    

    You need to make sure this is run before foreach. I modified your binding to do that: http://plnkr.co/edit/hS6Gb2xLfabSj9K8l03y?p=preview

    0 讨论(0)
提交回复
热议问题