2012-07-05 120 views
0

希望你能帮我解决一个jsTree问题我只有最近才开始使用它,所以仍然找到我的脚。jsTree删除节点实际上并没有删除它

我在同一页面上使用了两个jsTrees(左侧的可用对象和右侧的包含对象),我在中间有几个按钮让用户将对象从左侧的树移动到右侧的树反之亦然。我没有使用dnd插件,也不打算用户能够在树之间拖放。我的数据结构只有两层,父母和孩子/多个孩子。

另一件要提到的是,当我将一个节点从一棵树移到另一棵树时,它将从该树中移除并在另一棵树中创建。

因此,那么我遇到的问题是,我可以选择一个节点在左树(无论是父母或其子)之一,我可以将它移动到右边的树后点击按钮。如果我选择的节点是父节点,它将移动父节点和所有子节点。如果我选择的节点是唯一的孩子,它会移动父母和孩子。让我们继续坚持这个例子。我这样做是通过以下:

//Check to see if the parent exists in the right tree, if it doesn't create it 
I've left this function call out for now but can post later if required 
//Assuming the parent doesn't exist, create the parent in the right tree 
$("#treeInc").jstree("create", null, false, { attr: { id: parent.id }, data: $(parent).children("a").text() }, false, true); 
//Create the child in the right tree belonging to the parent node 
$("#treeInc").jstree("create", $("#" + parent.id), "inside", { attr: { id: child.id }, data: $(child).children("a").text() }, false, true); 

//Remove the parent and child from the left tree 
$("#treeAvail").jstree("remove", $('#' + child.id)); 
$("#treeAvail").jstree("remove", $('#' + parent.id)); 

现在,这一切的伟大工程,但我的问题是,当我尝试将节点移回到左树父似乎总是在甚至认为是应该的数据存在已被删除。这意味着我实际上不会调用代码来创建父项,而是仅创建子项,并且它最终会自行悬挂,并且没有父项。

所以我的逻辑是首先检查,以查看父母是否存在基本上在属于右树的节点中查找父ID。到目前为止,我已经完成了几种不同的方式,每次失败。

我已经调用左树的'get_json'函数来遍历节点,但它存在于那里。 我循环遍历最初加载左树的数组,但是它具有来自初始加载的所有节点,并且似乎永远不会改变。 我试过使用'delete_node'而不是删除,但这没有什么区别。

那么我做错了什么?如果我从jsTree中删除一个节点,就数据而言,我如何检查该树以查看它已经消失了?

请帮忙,它让我疯狂!

我的实际树数据来自一个web服务,但对于这个例子,下面的json数组会做。

availDataCache = [ 
     { attr: { id: "A_R1" } 
      , data: "Report 1" 
      , state: "open" 
      , children: [{ attr: { id: "A_PSA1" }, data: "Param Set A"}] 
     } 
     , { attr: { id: "A_R2" } 
      , data: "Report 2" 
      , state: "open" 
      , children : [ 
        { attr: { id: "A_PSA2" }, data: "Param Set A" } 
        ,{ attr: { id: "A_PSB1" }, data: "Param Set B" } 
        ] 
      } 
     ]; 

$("#treeAvail").jstree({ 
    "json_data": { 
     "data": availDataCache 
    }, 
    "core": { 
     "animation": 0 
    }, 
    "plugins": ["themes", "json_data", "ui", "core", "crrm"] 
}); 

$("#treeInc").jstree({ 
    "json_data": { 
     "data": incDataCache 
    }, 
    "core": { 
     "animation": 0 
    }, 
    "plugins": ["themes", "json_data", "ui", "core", "crrm"] 
}); 
+0

您使用的是什么版本的'jstree'?有些版本与其他版本非常不同。 – david 2012-07-05 17:49:36

+0

对不起,应该说...版本1.0-rc3,直接从网站下载 – 2012-07-05 20:36:03

回答

0

尝试在每次复制,移动,删除操作后调用jstree的刷新函数。

$("#leftjsTree").jstree("refresh" ,"#idOfLeftNode"); 
$("#rightjsTree").jstree("refresh" ,"#idOfRightNode");