2016-03-02 73 views
1

我試圖找到一種方法,我可以在加載動態上下文菜單(右鍵單擊)時在jstree中抑制changed事件。我知道你可以在上下文菜單中輸入suppress the select_node event,但我需要獲取我正確點擊的節點的節點ID。 (因此需要使用select_node)。我知道當您定期撥打select_node時,您可以撥打suppress that changed event,但我不確定如何在右鍵單擊時執行此操作。我試圖通過上下文菜單select_node以下,但沒有奏效:Jstree上下文菜單supress在select_node上改變了事件點擊右鍵

$(function() { 
    $('#myTree').jstree({ 
     "core": { 
      "themes": { 
       "variant": "small", 
       "icons": false 
      } 
     }, 
     "contextmenu": { 
      "items": reportMenu(node),  //builds context menu based on selected node 
     }, 
     "plugins": ["contextmenu", "changed"] 
    }); 
}); 
$("#myTree").bind('select_node.jstree', function (event, data) { 
// Does not work, changed event still fires. 
    $("#myTree").jstree().select_node(data.node.id, true); 
}); 

我在尋找可能的替代方案之一:

  1. 我怎樣才能抑制changed事件當背景菜單調用select_node
  2. 如何在不調用select_node事件(即如果將我的上下文菜單設置爲'select_node': false,如何捕獲選擇節點)的情況下右鍵點擊節點的id?
+1

順便說一句,爲什麼你不能在你的'reportMenu'函數中得到節點id作爲'node.id'? –

回答

3

最後,我想你可以得到你想要的改變你的代碼。

查看演示 - 。

$('#myTree') 
    .jstree({ 
     'core': { 
      'data': ... 
     }, 
     'plugins': ["contextmenu"], 
     'contextmenu': { 
      'select_node': false, 
      'items': reportMenu 
     } 
    }); 

function reportMenu(node) { 
    // access node as: node.id); 
    // build your menu depending on node id 
    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); } 
     } 
    }; 
} 
+0

我最初嘗試過這種方法,但不幸的是,我需要捕獲選定的節點,以便構建上下文菜單。不知道我可以做到這一點是我把select_node設置爲false? – usr4896260

+1

明白了。檢查更新的答案。 –

+0

其實你在上面的評論中是正確的。顯然,即使select_node爲false,我仍然可以捕獲node.id。謝謝澄清! – usr4896260

相關問題