jQuery插件TreeView异步加载树

前端jsp页面:

 

    <link rel="stylesheet" href="jquery.treeview.css" type="text/css"></link>
    <link rel="stylesheet" href="screen.css" type="text/css"></link>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.cookie.js"></script>
    <script type="text/javascript" src="jquery.treeview.js"></script>
    <script type="text/javascript" src="jquery.treeview.async.js"></script>
    <style type="text/css">
    a{
    	text-decoration:none;
    	color:#000000;
    }
    a:active{
    	color:#ff0000;
    }
    </style>
    <script type="text/javascript">
    	$(document).ready(function(){
    		$("#treeview").treeview({
    			url:"/treeviewtest/user/servlet/treeajax"
    		});
    	});
    </script>
  </head>
  
  <body>
    <ul id="treeview" class="filetree"></ul>
  </body>

 

后台代码:

 

String id = request.getParameter("root"); //treeview自动提交root参数为当前节点的id,如果是根节点,则为source

//生成json格式数据
private String toJSONString(Category category){
		StringBuilder sb = new StringBuilder();
		sb.append("{");
		sb.append("\"id\":\""+category.getCatId()+"\"");
		sb.append(",\"text\":\""+generateLinkString(category)+"\"");
		sb.append(",\"classes\":\""+category.getCssClass()+"\"");
		sb.append(",\"expanded\":false");
		sb.append(",\"hasChildren\":"+category.hasChild());
		if(!"".equals(category.getImageName())){
			sb.append(",\"imagepath\":\"url("+category.getImageName()+")\"");
//imagepath是自己加的,用于显示不同的图标,jquery.treeview.async.js文件需要修改
		}
		sb.append("}");
		return sb.toString();
	}

 

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

 

function load(settings, root, child, container) {
	$.getJSON(settings.url, {root: root}, function(response) {
		function createNode(parent) {
			var current = $("<li/>").attr("id", this.id || "").html("<span>" + this.text + "</span>").appendTo(parent);
			if (this.classes) {
				current.children("span").addClass(this.classes);
			}
			if(this.imagepath) {//这里是加的,后台代码是如果显示默认图标,没有imagepath这项,如果显示子定义图标,imagepath是图标的路径。
				current.children("span").css("background-image", this.imagepath);
			}
			if (this.expanded) {
				current.addClass("open");
			}
			if (this.hasChildren || this.children && this.children.length) {
				var branch = $("<ul/>").appendTo(current);
				if (this.hasChildren) {
					current.addClass("hasChildren");
					createNode.call({
						text:"placeholder",
						id:"placeholder",
						children:[]
					}, branch);
				}
				if (this.children && this.children.length) {
					$.each(this.children, createNode, [branch])
				}
			}
		}
		$.each(response, createNode, [child]);
        $(container).treeview({add: child});
    });
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值