jQuery插件TreeView异步加载树

给出两个官网,可以去看看教程。

http://docs.jquery.com/Plugins/Treeview
http://bassistance.de/jquery-plugins/jquery-plugin-treeview/

jsp页面:


Java代码   收藏代码
  1.   <link rel="stylesheet" href="jquery.treeview.css" type="text/css"></link>  
  2.   <link rel="stylesheet" href="screen.css" type="text/css"></link>  
  3.   <script type="text/javascript" src="jquery.js"></script>  
  4.   <script type="text/javascript" src="jquery.cookie.js"></script>  
  5.   <script type="text/javascript" src="jquery.treeview.js"></script>  
  6.   <script type="text/javascript" src="jquery.treeview.async.js"></script>  
  7.   <style type="text/css">  
  8.   a{  
  9.     text-decoration:none;  
  10.     color:#000000;  
  11.   }  
  12.   a:active{  
  13.     color:#ff0000;  
  14.   }  
  15.   </style>  
  16.   <script type="text/javascript">  
  17.     $(document).ready(function(){  
  18.         $("#treeview").treeview({  
  19.             url:"/treeviewtest/user/servlet/treeajax"  
  20.         });  
  21.     });  
  22.   </script>  
  23. </head>  
  24.   
  25. <body>  
  26.   <ul id="treeview" class="filetree"></ul>  
  27. </body>  

 

后台代码:

 

Java代码   收藏代码
  1. String id = request.getParameter("root"); //treeview自动提交root参数为当前节点的id,如果是根节点,则为source  
  2.   
  3. //生成json格式数据  
  4. private String toJSONString(Category category){  
  5.         StringBuilder sb = new StringBuilder();  
  6.         sb.append("{");  
  7.         sb.append("\"id\":\""+category.getCatId()+"\"");  
  8.         sb.append(",\"text\":\""+generateLinkString(category)+"\"");  
  9.         sb.append(",\"classes\":\""+category.getCssClass()+"\"");  
  10.         sb.append(",\"expanded\":false");  
  11.         sb.append(",\"hasChildren\":"+category.hasChild());  
  12.         if(!"".equals(category.getImageName())){  
  13.             sb.append(",\"imagepath\":\"url("+category.getImageName()+")\"");  
  14. //imagepath是自己加的,用于显示不同的图标,jquery.treeview.async.js文件需要修改  
  15.         }  
  16.         sb.append("}");  
  17.         return sb.toString();  
  18.     }  

 

修改jquery.treeview.async.js显示个性化图标:

 

Js代码   收藏代码
  1. function load(settings, root, child, container) {  
  2.     $.getJSON(settings.url, {root: root}, function(response) {  
  3.         function createNode(parent) {  
  4.             var current = $("<li/>").attr("id"this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);  
  5.             if (this.classes) {  
  6.                 current.children("span").addClass(this.classes);  
  7.             }  
  8.             if(this.imagepath) {//这里是加的,后台代码是如果显示默认图标,没有imagepath这项,如果显示子定义图标,imagepath是图标的路径。  
  9.                 current.children("span").css("background-image"this.imagepath);  
  10.             }  
  11.             if (this.expanded) {  
  12.                 current.addClass("open");  
  13.             }  
  14.             if (this.hasChildren || this.children && this.children.length) {  
  15.                 var branch = $("<ul/>").appendTo(current);  
  16.                 if (this.hasChildren) {  
  17.                     current.addClass("hasChildren");  
  18.                     createNode.call({  
  19.                         text:"placeholder",  
  20.                         id:"placeholder",  
  21.                         children:[]  
  22.                     }, branch);  
  23.                 }  
  24.                 if (this.children && this.children.length) {  
  25.                     $.each(this.children, createNode, [branch])  
  26.                 }  
  27.             }  
  28.         }  
  29.         $.each(response, createNode, [child]);  
  30.         $(container).treeview({add: child});  
  31.     });  
  32. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值