jsTree上下文菜单选定的项目?

3
我们正在使用jsTree(来自2011年9月2日的修订版236)。
有人知道是否有任何方法可以在与“操作”相关联的函数中访问选择的菜单项名称吗?
我想自动定义菜单项,以便每个菜单项的“操作”功能基于上下文菜单中项目的标识符进行设置。
例如,对于具有三个操作(“A”,“B”或“C”)的上下文菜单。
...
var items = {};             
for(var i=0; i < preconfiguredItemsData.length; i++) 
{ 
    var item = preconfiguredItemsData[i]; 

    items[item.name] = {
        "label": item.title,
        "action": function (liNode) {
            control = eval("new " + **SELECTED ITEM IDENTIFIER ?** + "()"); 
                    // **new A(), new B() or new C()** depending on the selected
                    // item on the context menu.
                    // I have the identifier of the jsTree node but ... how
                    // can I get the item id ("A", "B" or "C")?
            control.execute(); 
        },              
        "_class": "class",  
        "separator_before": false,
        "separator_after": true,
        "icon": false,
        "submenu": {}
    };
    ...
} //for

items.create = false; 
items.rename = false; 
items.remove = false,
items.edit = false;
items.ccp = false;

...

我希望我已经清楚地描述了我的问题。

提前感谢。

3个回答

3
我正在使用jsTree 3.0.2,但这个修复方法对我没用。
“i”参数已经包含在发送到“action”函数的结果中,但它只包含有关点击的上下文菜单的详细信息,而不是绑定到该jsTree分支的JSON项。
要获取右键单击的项目的id,这是我所做的事情。
我的树中的一些分支是文件夹,而另一些分支是报告(用户可以运行),因此我需要能够根据用户右键单击的节点类型来调整我的jsTree上下文菜单,并且对于报告,我需要获取所选记录的ID。
首先,我编写了一个小的getCustomMenu函数来获取特定jsTree分支的上下文菜单项,因此,我定义了以下jstree。
$('#divReportsTree').jstree({
   "core": {
       'data': JSON.Results.core.data
   },
   "plugins": ["contextmenu"],
   "contextmenu" : {
       //  Call a function, to fetch the CustomMenu for this particular jsTree branch
       items: getCustomMenu    
   }, 
})

我的getCustomMenu函数看起来像这样:

function getCustomMenu(node) {

    var thisReportID = node.li_attr.ReportID;

    var items = {
      Run: {
        "separator_before": false,
        "separator_after": true,
        "label": "Run this report",
        "icon": "/Images/Icons/Go.png",
        "action": function (node, reportID) {
             //  Call a function to run a report, with a particular Report ID 
             RunReport(node, thisReportID);
        }
      },
      Refresh: {
        "separator_before": false,
        "separator_after": false,
        "label": "Refresh",
        "icon": "/Images/Icons/Refresh.png",
        "action": function (node, reportID) {
             //  Call a refresh function, with a particular Report ID 
             RefreshReport(node, thisReportID);
        }
    };

    //  If this is a jsTree node for a Folder (rather than a Report) then 
    //  just show the "Refresh" ContextMenu item
    if (node.li_attr.ReportID == null)
    {
        delete items.Run;
    }

    return items;
}

当用户在我的中右键单击报表时,getCustomMenu函数将使用所选报表的ID调用我的RunReport函数。
这里的关键是JSON数据专门向jsTree的属性添加了ReportID属性。
由于我们的getCustomMenu调用操作函数(例如此示例中的"RunReport"),因此我们可以使其适应并向该函数传递额外参数。
希望这一切有所帮助(并且讲得通!)。

1
有一种更简单的解决方案,不需要修改jstree源代码。我使用 jstree 3.3.1 测试了这种方法。
文档(重点是我的)中得知:
一旦激活菜单项,将使用包含以下键的对象调用操作函数:item-如下所示的上下文菜单项定义、reference-使用的DOM节点(树节点)、element-上下文菜单DOM元素、position-一个带有x/y属性指示菜单位置的对象。 item属性是您上下文菜单项的完整定义。这意味着您可以将任何属性附加到该对象,并稍后检索其值。在问题示例中,这可能是_class属性。不清楚OP是否尝试过这种方法。
var items = {
  item1: {
    label: 'a label',
    icon: 'fa fa-font-awesome',
    separator_after: true,
    disabled: false,
    action: lang.hitch(this, 'doSomething'),
    _name: 'item1'
  }
}

然后,_name 将通过 item 属性传递。
doSomething: function (obj) {
  console.log(obj.item._name)
}

0

在原始的jquery.jstree.js函数调用中添加函数名称作为参数解决了问题。

(function ($) {
    $.vakata.context = {
            .....
            exec : function (i) {
                ....
                if($.isFunction($.vakata.context.func[i])) {
                    ....
                    $.vakata.context.func[i].call($.vakata.context.data, $.vakata.context.par, i);    //Param 'i' added        
                    ....
                }
                ....
            }
            ....
        }

谢谢!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接