2011-08-03 2 views
2

dijit.TreeForestStoreModel으로 데이터를 제공하기 위해 사용자 지정 데이터 저장소를 사용합니다. 잘 작동하지만 사용자가 dojo 끌어서 놓기 기능을 통해 최상위 항목 (및 최상위 항목 만)을 재정렬 할 수있는 기능을 제공하려고합니다.dijit 트리에서 최상위 노드 항목을 드래그 앤 드롭 방식으로 배열하는 방법

문제는 ForestStoreModel::pasteItem 함수가 루트의 하위 항목인지 확인한 다음 nullTreeStoreModel::pasteItem에 전달하는 것입니다. 전달 된 부모 항목이 null 인 경우 TreeStoreModel

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); 
     } 
    } 

는 기본 데이터 저장소를 업데이트하지 않고 onLeaveRootonAddToRoot 이벤트는 insertIndex에 통과하지 못한, 그래서 나는 것 같아 내 데이터 저장소 (업데이트하는 데 사용할 수 없습니다 어쨌든 조금 뒤쪽에있을 것입니다.) 이 시점에서

나는 유일한 실행 가능한 옵션이 나 호환 데이터 저장소 개체에 합성 $root$ 항목을 설정할 수 있도록하고 ForestStoreModelTreeStoreModel까지 변경되지 않은, 그것을 전달할 수 있도록하기 위해 ForestStoreModel을 확장하는 것입니다 생각합니다.

다른 방법으로이 문제가 발생합니까?

업데이트

궁극적 인 해결책은 제안보다 더 간단 밝혀졌다. 내 ForestStoreModel은 이미 dojo 1.6 ObjectStore를 데이터 소스로 사용하고 있으므로 사용자 정의 클래스이므로 options 인수의 원하는 인덱스를 객체 저장소 put 메소드에 전달할 수 있습니다.

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); 
} 
+0

이 업데이트는 답변 일 것입니다. – DanMan

답변

1

당신은 ForestTreeModel를 서브 클래 싱해야합니다, 당신은 그것을 벗어날 수 없다 : 나는 onLeaveRootonAddRoot를 호출 부모 클래스 돌봐하자로 수정 그냥 한 줄이었다. 그러나 pasteItem 만 재정의하면됩니다. 합성 루트는 TreeStoreModel에 전달할 수 없습니다. 왜냐하면 그것에 대해 아무 것도 모르기 때문입니다.

기본 데이터 저장소를 수정해야하는 경우 this.store.setValues()을 직접 호출하는 것이 좋습니다. 그러면 onSetItem 이벤트가 발생하고 차례로 _requeryTop()이 호출되어 정렬 순서에 관계없이 기본 저장소에서 루트를 가져 오므로 변경 사항에 반영해야합니다.

dojo.declare('MyForestTreeModel', [ dijit.tree.ForestTreeModel ], { 
    pasteItem: function(childItem, oldParentItem, newParentItem, bCopy, insertIndex) { 
    if (oldParentItem == this.root && newParentItem == this.root) { 
     if (!bCopy) { this.onLeaveRoot(childItem); } 
     // modify the underlying store somehow so the call to _requeryTop() fetches 
     // the items in the correct order. 
     // (you decide what's 'order' and what new_order() returns) 
     this.store.setValues(childItem, 'order', new_order(insertIndex)); 
     this.onAddRoot(childItem); 
    } else { 
     // call super 
     this.inherited(arguments); 
    } 
    } 
}); 

다른, 쉬운 방법은 뷰 통지 이벤트 onChildrenChange을 방출 한 후 this.root.children 직접 조작하고있다. 이 방법은 주문을 유지하지 않습니다.

dojo.declare('MyForestTreeModel', [ dijit.tree.ForestTreeModel ], { 
    pasteItem: function(childItem, oldParentItem, newParentItem, bCopy, insertIndex) { 
    if (oldParentItem == this.root && newParentItem == this.root) { 
     if (!bCopy) { this.onLeaveRoot(childItem); } 
     // manipulate this.root.children to reorder childItem 
     // remove child from the current position 
     var children = dojo.filter(this.root.children, function(x) { 
     return x != childItem; 
     }); 
     // and insert it into the new index 
     children.splice(insertIndex, 0, childItem); 
     this.root.children = children; 
     // notify views 
     this.onChildrenChanged(this.root, children); 
     this.onAddRoot(childItem); 
    } else { 
     // call super 
     this.inherited(arguments); 
    } 
    } 
}); 
+0

데이터 저장소를 수정해야하므로 첫 번째 제안을 기반으로 한 버전을 사용해 보겠습니다. – Lucas