2012-07-05 123 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");