【发布时间】:2011-08-03 20:01:26
【问题描述】:
我有一个 dijit.Tree 支持一个 ForestStoreModel,它使用自定义数据存储来提供数据。它工作得很好,但我想为用户提供通过 dojo 拖放功能重新排列顶级项目(并且只有顶级)项目的能力。
问题在于ForestStoreModel::pasteItem 函数检查项目是否是根的子项,然后将null 传递给TreeStoreModel::pasteItem。
pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy, /*int?*/ insertIndex){
// summary:
// Move or copy an item from one parent item to another.
// Used in drag & drop
if(oldParentItem === this.root){
if(!bCopy){
// It's onLeaveRoot()'s responsibility to modify the item so it no longer matches
// this.query... thus triggering an onChildrenChange() event to notify the Tree
// that this element is no longer a child of the root node
this.onLeaveRoot(childItem);
}
}
dijit.tree.TreeStoreModel.prototype.pasteItem.call(this, childItem,
oldParentItem === this.root ? null : oldParentItem,
newParentItem === this.root ? null : newParentItem,
bCopy,
insertIndex
);
if(newParentItem === this.root){
// It's onAddToRoot()'s responsibility to modify the item so it matches
// this.query... thus triggering an onChildrenChange() event to notify the Tree
// that this element is now a child of the root node
this.onAddToRoot(childItem);
}
}
如果传递的父项为空并且onLeaveRoot 和onAddToRoot 事件未传入insertIndex,则TreeStoreModel 不会更新基础数据存储,因此我无法使用它们来更新我的数据存储(无论如何,这似乎有点倒退)。
在这一点上,我认为唯一可行的选择是扩展 ForestStoreModel 以允许我将合成的 $root$ 项目设置为兼容的数据存储对象并允许 ForestStoreModel 传递它,不变,到TreeStoreModel。
还有其他方法可以解决这个问题吗?
更新
最终的解决方案比建议的还要简单。我的ForestStoreModel 已经是一个自定义类,因为我实际上使用dojo 1.6 ObjectStore 作为数据源,所以我可以将options 参数中所需的索引传递给对象存储put 方法。修复只是一条线,因为我让父类负责调用 onLeaveRoot 和 onAddRoot:
pasteItem: function(/*Item*/ childItem, /*Item*/ oldParentItem, /*Item*/ newParentItem, /*Boolean*/ bCopy, /*int?*/ insertIndex){
// Handle drag & drop at the root level
if (oldParentItem === this.root && newParentItem === this.root){
this.store.put(childItem, { index: insertIndex });
}
this.inherited(arguments);
}
【问题讨论】:
-
那个更新应该是一个答案。
标签: drag-and-drop tree dojo