1

我爲我的頁面使用Knockout.js。我的ViewModel包含一個對象數組。每個對象都包含一個Childs數組。更一般的這應該表示一個包含列和列內容的表。Knockout-Sortable catch複製對象

首先我正在處理foreach:itemArray來獲取列。然後我使用一個嵌套的foreach:childs,childs是列內容的數組。

我的孩子應該可以在列之間拖動。因此,我替換一個js嵌套的foreach我發現

https://github.com/rniemeyer/knockout-sortable

http://jsfiddle.net/rniemeyer/Jr2rE/

再次用代碼 - 第一階段:

<div id="lanesContainer" data-bind="foreach: lanes"> 

然後按照嵌套的foreach(內#lanesContainer)

<ul data-bind="sortable: { template: 'laneTemplate', data: childs, afterMove: $root.dropCallback }"> 

我的項目現在可以拖動,但是拖放無法完成。我的調試器中斷在JS的以下部分:

//take destroyed items into consideration 
if (!templateOptions.includeDestroyed) { 
    targetUnwrapped = targetParent(); 
    for (i = 0; i < targetIndex; i++) { 
    //add one for every destroyed item we find before the targetIndex in the target array 
    if (targetUnwrapped[i] && targetUnwrapped[i]._destroy) { 
     targetIndex++; 
    } 
    } 
} 

它打破了在第三行,因爲targetParent是一個對象,不是一個函數。我該如何解決這個問題?

回答

0

OK感謝至今。 我的ViewModel包含一個函數,它在每個drop上被調用。

self.dropCallback = function (arg) { 
    var item = arg.item; 
    var sourceParent = arg.sourceParent(); 
    var targetParent = arg.targetParent(); 
    blah(); //console.log("Moved '" + arg.item.name() + "' from " + arg.sourceParent.id + " (index: " + arg.sourceIndex + ") to " + arg.targetParent.id + " (index " + arg.targetIndex + ")"); 
}; 

變量sourceParent和targetParent現在代表用於foreach的數組。我怎麼能'走'一個更高的水平,到下一個家長?

+0

好吧,它是sourceParentNode.parent() – redflag237 2013-02-21 11:25:29

1

變化targetUnwrapped = targetParent();

targetUnwrapped = ko.utils.unwrapObservable(targetParent);