2012-09-04 58 views
4

我想用jstree的contextmenu捕獲新創建的節點的名稱。我可以捕獲我在(使用obj.text())下添加新節點的父節點的名稱,但是,我真正需要的是新創建節點的名稱。用JSTree的contextmenu捕獲新創建的節點

因此,在某種程度上,需要有一個「onChange」事件,可以在jstree contextmenu中調用,一旦用戶點擊進入該新創建的節點,就會觸發該事件?

任何想法?我已經包圍的文本菜單代碼:

}).jstree({ 
     json_data: { 
      data: RBSTreeModel, 
      ajax: { 
       type: "POST", 
       data: function (n) { 
        return { 
         NodeID: n.attr("id").substring(4), 
         Level: n.attr("name").substring(7) 
        }; 
       }, 
       url: function (node) { 
        return "/Audit/GetRequirementsTreeStructure"; 
       }, 
       success: function (new_data) { 
        return new_data; 
       } 
      } 
     }, 
     contextmenu: { 
      items: function($node) { 
       return { 
        createItem : { 
         "label" : "Create New Branch", 
         "action" : function(obj) { this.create(obj); alert(obj.text())}, 
         "_class" : "class" 
        }, 
        renameItem : { 
         "label" : "Rename Branch", 
         "action" : function(obj) { this.rename(obj);} 
        }, 
        deleteItem : { 
         "label" : "Remove Branch", 
         "action" : function(obj) { this.remove(obj); } 
        } 
       }; 
      } 
     }, 
     plugins: ["themes", "json_data", "ui", "crrm", "contextmenu"] 
    }); 

回答

6

您可以綁定到「create.jstree」事件,將創建一個節點之後,將觸發。在該事件的回調中,您將有權訪問新創建的節點,並且可以回滾/恢復創建節點操作(如果您選擇)。它的文檔缺乏,但在demo page上有一個例子。下面是從我的代碼來了另外一個例子:

}).jstree({... You jstree setup code...}) 
     .bind("create.jstree", function(e, data) { 
      // use your dev tools to examine the data object 
      // It is packed with lots of useful info 
      // data.rslt is your new node 
      if (data.rslt.parent == -1) { 
       alert("Can not create new root directory"); 
       // Rollback/delete the newly created node 
       $.jstree.rollback(data.rlbk); 
       return; 
      } 
      if (!FileNameIsValid(data.rslt.name)) { 
       alert("Invalid file name"); 
       // Rollback/delete the newly created node 
       $.jstree.rollback(data.rlbk); 
       return; 
      } 
      .. Your code etc... 
     }) 
+0

真棒!感謝您及時的回覆! – TheDude

+0

謝謝,正是我在找的東西。 jstree文檔有改進的地方。 – winkbrace

+0

@Bojin Li rollback()api似乎在最新的jstree版本中缺失。你能建議一個回滾的替代方案嗎? –

2

基於博進李的回答,似乎jsTree的最新版本使用,而不是「創造」事件「create_node」:

 

}).jstree({... You jstree setup code...}) 
     .bind("create_node.jstree", function(e, data) { 
     ... 
     }); 

+0

奇怪的是,當我嘗試綁定到create_node.jstree事件時,它在實際創建新節點之前觸發,所以我得到的文本始終是「新節點」,而不是用戶想要給它的任何名稱。 。你知道如何捕獲新建立的節點嗎? –

+0

我認爲你必須使用rename_node.jstree,如[這裏]所述(https://stackoverflow.com/questions/31733744/jstree-get-new-node-after-node-is-created) –