dojo lazy Tree,使用JsonRest获取节点数据

创建html标签

<div id="tree2"></div>

创建dojo代码

			require([
				"dojo/store/JsonRest",
				"dijit/Tree",
				"dijit/tree/ObjectStoreModel",
				"dojo/domReady!"
			], function(JsonRest, Tree, ObjectStoreModel){

				var usGov = new JsonRest({
					target: "/dojo/rest/echo/getLazyTree",// dojo为tomcat中配置的Context path,rest为web.xml配置路径
					getChildren: function(object){
			            return this.query({id: object.id});
			        }
				});
				var model = new ObjectStoreModel({
					store: usGov,
					
					mayHaveChildren: function(object){
						if(object.children == false){// 为叶子节点
							return false;
						}else{// 非叶子节点
							return "children" in object;	
						}
						
					}
				});

				var tree = new Tree({
					model: model,
					persist: false
				}, "tree2"); 
				tree.startup();
			});

rest服务的java代码

bean类

	class Org{
		String id;
		String name;
		boolean children;
		
		public String getId() {
		
			return id;
		}
		
		public void setId( String id ) {
		
			this.id = id;
		}
		
		......
	}
rest类

@Path("/echo")
public class Echo {
	@GET
	@Path("/getLazyTree")
	@Produces("application/json")
	public List< Org > getTreeParent(@Context HttpServletRequest request){
		
		String id = request.getParameter( "id" );
		System.out.println("id="+id);
		
		if( id == null ){
			Org root = new Org();
			root.setName( "北京测试" );
			root.setId( "root" );
			root.setChildren( true );
			List< Org > lt = new ArrayList< Org >();
			lt.add( root );
			return lt;
		}else{
			if( id.equals( "root" ) ){
				Org one = new Org();
				one.setId( "corp1" );
				one.setName( "测试子公司1" );
				one.setChildren( true );
				
				Org two = new Org();
				two.setId( "corp2" );
				two.setName( "测试子公司2" );
				two.setChildren( true );
				
				List< Org > subOrgs = new ArrayList< Org >();
				subOrgs.add( one );
				subOrgs.add( two );
				
				return subOrgs;
			}
			
			if( id.equals( "corp1" ) ){
				Org one = new Org();
				one.setId( "team1" );
				one.setName( "测试车队1" );
				one.setChildren( false );
				
				Org two = new Org();
				two.setId( "team2" );
				two.setName( "测试车队2" );
				two.setChildren( false );
				
				List< Org > subOrgs = new ArrayList< Org >();
				subOrgs.add( one );
				subOrgs.add( two );
				
				return subOrgs;
			}
			
			if( id.equals( "corp2" ) ){
				Org one = new Org();
				one.setId( "team21" );
				one.setName( "测试车队21" );
				one.setChildren( false );
				
				Org two = new Org();
				two.setId( "team22" );
				two.setName( "测试车队22" );
				two.setChildren( false );
				
				List< Org > subOrgs = new ArrayList< Org >();
				subOrgs.add( one );
				subOrgs.add( two );
				
				return subOrgs;
			}
		}
		
		return null;
	}
} 

从头信息看调用过程及返回数据

加载跟节点


加载第一级子节点


调用完成后的页面


点击第一级子节点后的调用过程


调用完成后的页面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值