javascript - 如何判断节点类型

标签 javascript jstree

我们有一个可以有不同类型节点的 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,result 是具有以下结构的字典:

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

最佳答案

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

我认为这篇博文对这个问题有有值(value)的信息: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 () { ... }
       };
   }

如果特定节点已关闭(因此在其类列表中有“jstree-closed”类),给定的代码片段只会将删除功能添加到上下文菜单。

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

您可以像下面这样使用这些 API 调用:

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

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

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

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

关于javascript - 如何判断节点类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6815232/

相关文章:

jquery - 是否可以从jstree拖放到jqgrid?

c# - 解析器识别正则表达式时出错

Javascript 的 getElementById 和 innerHTML 不能间接工作

javascript - 有没有办法在给定的持续时间内更改跨度文本的颜色?

javascript - 使用 selenium-webdriver 扩展 jstree 的所有节点

javascript - 将节点从一个 jstree 移动到另一个 jstree 不起作用

javascript - IIS 7.5 没有图像 css js 显示

javascript - 使用javascript控制嵌入式windows media player

jquery - 如何将数据关联到jstree中的节点?

php - jsTree 多树 MySQL/PHP 实现 - 嵌套集