Fancytree实例
一、实例
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>fancyTreeTest</title> <script src="jquery.js"></script> <script src="jquery-ui.min.js"></script> <link href="ui.fancytree.min.css" rel="stylesheet" /> <script src="jquery.fancytree-all.min.js"></script> <script type="text/javascript"> $(function () { //Create the tree inside the <div id="tree"> element.与zTree类似 // 基础配置,返回数据包括 $("#tree") .fancytree({ source: [ { "title": "Node 1", "key": "1" }, { "title": "Folder 2", "key": "2", "folder": true, "children": [ { "title": "Node 2.1", "key": "3" }, { "title": "Node 2.2", "key": "4" } ] } ] }); }); </script> <script> $.ui.fancytree.debugLevel = 1; // silence debug output function logEvent(event, data, msg) { // var args = $.isArray(args) ? args.join(", ") : msg = msg ? ": " + msg : ""; $.ui.fancytree.info("Event('" + event.type + "', node=" + data.node + ")" + msg); } $(function () { $("#tree") .fancytree({ checkbox: true, // --- Tree events ------------------------------------------------- blurTree: function (event, data) { logEvent(event, data); }, create: function (event, data) { logEvent(event, data); }, init: function (event, data, flag) { logEvent(event, data, "flag=" + flag); }, focusTree: function (event, data) { logEvent(event, data); }, restore: function (event, data) { logEvent(event, data); }, // --- Node events ------------------------------------------------- activate: function (event, data) { logEvent(event, data); var node = data.node; // acces node attributes $("#echoActive").text(node.title); if (!$.isEmptyObject(node.data)) { // alert("custom node data: " + JSON.stringify(node.data)); } }, beforeActivate: function (event, data) { logEvent(event, data, "current state=" + data.node.isActive()); // return false to prevent default behavior (i.e. activation) // return false; }, beforeExpand: function (event, data) { logEvent(event, data, "current state=" + data.node.isExpanded()); // return false to prevent default behavior (i.e. expanding or collapsing) // return false; }, beforeSelect: function (event, data) { // console.log("select", event.originalEvent); logEvent(event, data, "current state=" + data.node.isSelected()); // return false to prevent default behavior (i.e. selecting or deselecting) // if( data.node.isFolder() ){ // return false; // } }, blur: function (event, data) { logEvent(event, data); $("#echoFocused").text("-"); }, click: function (event, data) { logEvent(event, data, ", targetType=" + data.targetType); // return false to prevent default behavior (i.e. activation, ...) //return false; }, collapse: function (event, data) { logEvent(event, data); }, createNode: function (event, data) { // Optionally tweak data.node.span or bind handlers here logEvent(event, data); }, dblclick: function (event, data) { logEvent(event, data); // data.node.toggleSelect(); }, deactivate: function (event, data) { logEvent(event, data); $("#echoActive").text("-"); }, expand: function (event, data) { logEvent(event, data); }, focus: function (event, data) { logEvent(event, data); $("#echoFocused").text(data.node.title); }, keydown: function (event, data) { logEvent(event, data); switch (event.which) { case 32: // [space] data.node.toggleSelected(); return false; } }, keypress: function (event, data) { // currently unused logEvent(event, data); }, lazyLoad: function (event, data) { logEvent(event, data); // return children or any other node source data.result = { url: "ajax-sub2.json" }; // data.result = [ // {title: "A Lazy node", lazy: true}, // {title: "Another node", selected: true} // ]; }, loadChildren: function (event, data) { logEvent(event, data); }, loadError: function (event, data) { logEvent(event, data); }, postProcess: function (event, data) { logEvent(event, data); // either modify the ajax response directly data.response[0].title += " - hello from postProcess"; // or setup and return a new response object // data.result = [{title: "set by postProcess"}]; }, removeNode: function (event, data) { // Optionally release resources logEvent(event, data); }, renderNode: function (event, data) { // Optionally tweak data.node.span // $(data.node.span).text(">>" + data.node.title); logEvent(event, data); }, renderTitle: function (event, data) { // NOTE: may be removed! // When defined, must return a HTML string for the node title logEvent(event, data); // return "new title"; }, select: function (event, data) { logEvent(event, data, "current state=" + data.node.isSelected()); var s = data.tree.getSelectedNodes().join(", "); $("#echoSelected").text(s); } }) .bind("fancytreeactivate", function (event, data) { // alternative way to bind to 'activate' event logEvent(event, data); }) .on("mouseenter mouseleave", ".fancytree-title", function (event) { // Add a hover handler to all node titles (using event delegation) var node = $.ui.fancytree.getNode(event); node.info(event.type); }); $("#btnSelect") .click(function (event) { var node = $("#tree").fancytree("getActiveNode"); node.setSelected(!node.isSelected()); }); $("#btnRemove") .click(function (event) { var node = $("#tree").fancytree("getActiveNode"); node.remove(); }); }); </script> </head> <body> [...] <!-- show tree --> <div id="tree">...</div> [...] </body> </html>
总结:1)将body中的div换成table就会出现上边不同的结果,最右边结果
2)对数的具体操作:
//获取Id为tree的Fancytree对象 $("#tree").fancytree("getTree") //获取tree的根节点 $("#tree").fancytree("getRootNode") //访问每个节点并把节点展开 $("#tree").fancytree("getRootNode").visit(function(node) { node.setExpanded(true);
3)参考大牛:http://wwwendt.de/tech/fancytree/demo/
官方文档下载:http://wwwendt.de/tech/fancytree/demo/