dijit.Tree DnD从树到树 - 子节点不会在目标树中创建

时间:2011-08-11 20:51:38

标签: javascript tree dojo drag-and-drop

我有两个dijit.Trees:srcTree和trgTree。 我已经实现了trgTree.itemCreator函数,并且在目标节点/树中正确创建了被删除的节点,但是没有创建被删除节点的子节点。我已经尝试了许多排列来创建它们,但似乎没有任何效果。

对我所缺少的内容有任何见解。这是我的itemCreator代码:

    trgTreeItemCreator: function(/*DomNode[]*/ nodes, target, /*dojo.dnd.Source*/ source) {
    console.info("*** start newTreeItemCreator");
    var tmp = dijit.getEnclosingWidget(nodes[0]);

    var kids = dojo.map(tmp.item.items, function(kid){
        return {
            "id": kid.id,
            "name": [kid.name[0]],
            "type": [kid.type[0]]
        };
    });

    var newItems = [{id:nodes[0].id, name:[tmp.label], type:[tmp.item.type[0]], items:kids}];
    return newItems;
},

1 个答案:

答案 0 :(得分:2)

我的帖子赢得了风滚草......真棒! :) 我想出了一个解决方法,就在这里。

    var that = this;
    this.trgTree = 
        new dijit.Tree({
            model: this.trgTreeModel,
            dndController: "dijit.tree.dndSource",
            checkAcceptance: this.treeCheckAcceptance,
            checkItemAcceptance: this.trgTreeCheckItemAcceptance,
            getIconClass: app.getIcon,
            onDndDrop: function(source, nodes, copy) {
            that.handleDrop(that, dijit.getEnclosingWidget(
                nodes[0]).item, this.current.item);
            that.srcStore.save();
            that.trgStore.save();
        }
    }, "trgTree");

    handleDrop: function(inPage, inSrcItem, inTrgItem) {
        console.info("*** start addItem: srcItem: " + inSrcItem.name[0]);
        var srcType = inSrcItem.type[0];
        var kids = inSrcItem.items;

        if (inTrgItem.type != "fooType") {
            // do something unique for fooType node targets
        }

    // add the item to the tree
    inPage.addItem(inPage, inSrcItem, inTrgItem);
}

addItem: function(inPage, inSrcItem, inTrgItem) {
    var srcType = inSrcItem.type[0];
    var trgType = inTrgItem.type[0];

    var theName = inSrcItem.name[0];

    if (srcType == "fooType") {
        srcType = inSrcItem.txType[0];
    }

    var newItem = {
        id: inSrcItem.id[0],
        name: theName,
        type: srcType,
        items: []
    };


    newItem = inPage.trgStore.newItem(newItem);
    inPage.trgTreeModel.pasteItem(newItem, null, inTrgItem, false, 0);

    var kids = inSrcItem.items;
    if (kids !== undefined) {
        // call recursively to add child items
        for (var i in kids) {
            inPage.addItem(inPage, kids[i], newItem);
            // TODO: expand the node after it is added
        }
    }
},

所有这些代码都是在单个类中的上下文,其中这是该类(这是一个WaveMaker项目,因此该类是一个wm.Page)。 很抱歉缺乏详细信息和文档,但它是很好的入门代码。如果您对我的解决方法有具体问题,请询问,我会尝试及时回答。