gpt4 book ai didi

javascript - KnockoutJS/jQuery UI 可排序冲突

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:40 24 4
gpt4 key购买 nike

我正在尝试同时使用 KnockoutJS 和 jQuery UI Sortable。我知道这已经完成了(特别是 knockout-sortable ),但我的用例有一些非常具体的行为,我希望避免尝试进行切换。

无论如何,问题非常简单——在使用 jQuery UI Sortable 移动 DOM 元素后,Knockout 在移除绑定(bind)到该 DOM 元素的 observableArray 元素时表现异常。它将无法移除被移动的元素,并且如果落入被移动元素位置的元素被移除,它将同时移除该元素和最初被移动的元素。难以用语言表达,但可以通过 this fiddle 证明.

问题似乎实际上发生在 knockout-2.1.0.js 的以下 block 中:

function fixUpVirtualElements(contiguousNodeArray) {
// Ensures that contiguousNodeArray really *is* an array of contiguous siblings, even if some of the interior
// ones have changed since your array was first built (e.g., because your array contains virtual elements, and
// their virtual children changed when binding was applied to them).
// This is needed so that we can reliably remove or update the nodes corresponding to a given array item

if (contiguousNodeArray.length > 2) {
// Build up the actual new contiguous node set
var current = contiguousNodeArray[0], last = contiguousNodeArray[contiguousNodeArray.length - 1], newContiguousSet = [current];
while (current !== last) {
current = current.nextSibling;
if (!current) // Won't happen, except if the developer has manually removed some DOM elements (then we're in an undefined scenario)
return;
newContiguousSet.push(current);
}

// ... then mutate the input array to match this.
// (The following line replaces the contents of contiguousNodeArray with newContiguousSet)
Array.prototype.splice.apply(contiguousNodeArray, [0, contiguousNodeArray.length].concat(newContiguousSet));
}
}

此调用将移动的 DOM 元素添加到要删除的元素列表中,当移动的元素被移除时。

因此,向任何 jQuery UI/Knockoutjs 天才公开征求意见 - 是否有解决此冲突的方法,或者我是否需要做一些完全不同的事情才能使这些工具很好地协同工作?

最佳答案

我认为“最佳”解决方案是从 DOM 中删除该元素并更改其在 KO 中的位置。您可以在 sortable 的 stop 事件中执行此操作。 http://jsfiddle.net/vgrTY/4/

我继续将您的 array-contents 文本更改为计算文本,以便正确显示。

关于javascript - KnockoutJS/jQuery UI 可排序冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12080839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com