如何在jsTree中双击/单击打开/关闭节点

时间:2011-05-05 01:26:09

标签: jquery jstree

如何在节点名称的双重或单个点击打开/关闭节点?就像它在这里工作the first tree sample - 但是使用了jsTree 0.9.8

-

<html>
<head>
<title> dashboard</title>

<script type="text/javascript" src="_lib/jquery.js"></script>
<script type="text/javascript" src="jquery.jstree.js"></script>
<script type="text/javascript" src="_lib/jstreegrid.js"></script>
<script type="text/javascript">
//<![CDATA[

$(document).ready(function(){

var data = [{
       data: "basics",
       attr: {SOF: "<a href=\"http://www.w3schools.com\">Visit W3Schools.com!</a>"},  
        children: [
         {data: "login", attr: {run: "run"},
           children: [                   
           {data: "login", attr: {}}
          ]
         } ,
         {data: "Academic Year", attr: {run: "run"},
          children: [                   
           {data: "login", attr: {}},
           {data: "Academic Year",  attr: {filter: "mini", SOF: "<a href=\"http://www.w3schools.com\">Visit W3Schools.com!</a>"}}
          ]

         }
        ]
      }];
$("div#jstree").jstree({
plugins: ["themes","json_data","grid","dnd"],
json_data: {data: data},
grid: {
        columns: [
          {width: 220, header: "Group"},
                        {cellClass: "col2", value: "run", width: 40, header: "run"},
                        {cellClass: "col3", value: "filter", width: 40, header: "filter"},
                        {cellClass: "col4", value: "SOF", width: 450, header: "SOF"}
                ]
            },
dnd: {
drop_finish : function () {
},
drag_finish : function () {
},
drag_check : function (data) {
return {
after : true,
before : true,
inside : true
};
}
}
});
});
//]]>
</script>
</head>
<body>
<div id="jstree"></div>

</body>
</html>

4 个答案:

答案 0 :(得分:11)

一种方法是启用类型和ui插件,并在默认类型上定义一个select_node事件处理程序,如下所示:

   $(element)
        .jstree({ 
            "types" : { 
                "types" : { 
                    "default" : { 
                        "select_node" : function(e) {
                                            this.toggle_node(e);
                                            return false;
                                        } 

                    }
                } 
             },
             "plugins" : [ "themes", "html_data","types", "ui" ] });

答案 1 :(得分:2)

除了传统知识的正确答案......

当您单击具有href属性的树项目(由JSON,XML数据或直接在HTML中设置)时,此解决方案将破坏导航。

要解决此问题,在叶子配置的“类型”(锚点必须触发导航)中设置此处理程序:

"select_node": function (e) {
   document.location = e.children("a").attr("href");
   return false;
}

答案 2 :(得分:1)

创建树时,您不必添加clickable: [...]选项吗?

答案 3 :(得分:0)

$('#domainvariants').jstree({
    plugins : ["themes","html_data","ui","crrm"]
}).bind("select_node.jstree", function (event, data) {
    return data.instance.toggle_node(data.node);
});