【问题标题】:How to determine the type of node如何确定节点的类型
【发布时间】:2011-07-25 11:17:35
【问题描述】:

我们有一个可以有不同类型节点的 JSTree,我必须为每个节点创建一个自定义菜单。

我已经到了这一点:

<script type="text/javascript">
    // Initialization function for JTree. We can not mve this in a different file, because Genshi placeholders won't be replaced there.             
    $(function () {
        $("#tree4").jstree({ 
            contextmenu: {
                "items" : createDefaultMenu
        },
            "plugins": ["themes", "json_data", "ui", "crrm", "contextmenu"],
            "json_data": {"ajax": { url: "/project/readjsonstructure/${project.id}",
                                    success: function (d) { return eval(d); }
                                   }}
            });
        });

    function createDefaultMenu(obj)
    {   
        return { 
            create: false, 
            rename: false, 
            remove: false, 
            ccp: false, 
            launch_viewer: {
                        label: "Launch viewer",
                        action: function (obj) { 
                                alert('Should launch here.')
                                               },
                        seperator_after: false,
                        seperator_before: false
                    }
            }            
    }
</script>

现在从我所读到的 obj 应该包含 JSTree 中当前单击的节点(如果我错了,请纠正我)。但是现在我如何找出这是什么类型的节点呢? readjson 结构返回一个:

encoder = JSONEncoder()
return encoder.iterencode(result)

其中 JSONEncoder 来自 simplejson,结果是具有以下结构的字典:

{ data: {
    title: "root",
        icon: "/static/style/nodes/nodeRoot.png"},
    state:"open", 
    attr:{id:"projectID"}, 
    children: [ data { .....

【问题讨论】:

    标签: javascript jstree


    【解决方案1】:

    我只是偶然发现了这个很老的问题,但也许我的信息对其他人仍然有价值。

    我认为这篇博文为这个问题提供了有价值的信息:jsTree: different contextmenu actions for different nodes

    以下几行显示了如何通过识别节点的类来获取节点的类型:

       var items = {};
       if ($(node).hasClass("jstree-closed")) {
           //build the items object like you want it
           items = "delete" : {
              "label" : "Delete File",
              "action" : function () { ... }
           };
       }
    

    给定的代码 sn-p 仅在特定节点关闭时将删除功能添加到上下文菜单(因此在其类列表中有“jstree-closed”类)。

    实际上这是一种非常肮脏的方式,因为 jsTree-API 允许您直接向它询问节点的状态 (jsTree API)。 问题是,您想使用什么标准将某个上下文菜单应用于节点。一些可能性是:

    您可以使用这些 API 调用,如下所示:

    $('#treeId').jstree().is_disabled(node);
    

    节点是 jQuery-Object,您可以使用$('#nodeId') 接收它,也可以使用function createDefaultMenu(obj) 的参数。因此,您可以像这样识别节点是启用还是禁用:

    var node = $('#nodeId')
    $('#treeId').jstree().is_disabled(node);
    

    我希望这对偶然发现它的人有所帮助。如果我犯了错误,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2013-07-23
      • 1970-01-01
      相关资源
      最近更新 更多