Ext同步加载树!

1、Action后台java代码
  	/**
	 * 显示树形  【left左边的导航】
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	public ActionForward showTreeNode(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		
		List<Menu> menulist = new ArrayList<Menu>();
		//Menu zonemenu = null;
		
		//区域
		List<ZoneInfo> zoneList = zoneInfoService.getZoneInfoList();
		for (ZoneInfo zone : zoneList) {
			Menu zonemenu = new Menu();
			zonemenu.setId("zone"+zone.getZoneId());
			zonemenu.setText(zone.getZoneName());
			
			List<MerchantInfo> merchanglist = merchantInfoService.getMerchantInfoByZoneID(zone.getZoneId());//根据区域ID显示商户
			if(merchanglist.size()>0)
			{
				zonemenu.setLeaf(false);
				
				List<Menu> merchantlistmenu = new ArrayList<Menu>();
				zonemenu.setChildren(merchantlistmenu);//区域添加商户
				for (MerchantInfo merchant : merchanglist) {
					Menu merchantmenu = new Menu();
					merchantmenu.setId("merchant"+merchant.getId());
					merchantmenu.setText(merchant.getMerchantForShort());
					
					
					List<StoreInfo> storeinfo = merchantInfoService.getStoreInfoByMerchantID(merchant.getId());//根据商户id得到门店
					if(storeinfo.size()>0)
					{
						merchantmenu.setLeaf(false);
						
						List<Menu> storelistmenu = new ArrayList<Menu>();
						merchantmenu.setChildren(storelistmenu);//商户添加门店
						for (StoreInfo store : storeinfo) {
							Menu storemenu = new Menu();
							storemenu.setId("store"+store.getId());
							storemenu.setText(store.getStoreForShort());
							
							List<StationInfo> stationlist = stationInfoService.getStationInfoByStoreID(store.getId());//根据门店ID得到站点
							
							if(stationlist.size()>0)
							{
								storemenu.setLeaf(false);
								
								List<Menu> stationlistmenu = new ArrayList<Menu>();
								storemenu.setChildren(stationlistmenu);//门店添加站点
								for (StationInfo station : stationlist) {
									Menu stationmenu = new Menu();
									stationmenu.setId("station"+station.getId());
									stationmenu.setText(station.getStationCode());
									stationmenu.setLeaf(true);
									stationlistmenu.add(stationmenu);
								}
							}else{
								storemenu.setLeaf(true);
							}

							storelistmenu.add(storemenu);
						}
					}else{
						merchantmenu.setLeaf(true);
					}

					merchantlistmenu.add(merchantmenu);
				}
			}else{

				zonemenu.setLeaf(true);
			}
			menulist.add(zonemenu);
			
		}
		JSONArray json = JSONArray.fromObject(menulist);
		response.getWriter().print(json.toString());
		
		return null;
	}


2、Menu.class类
  package com.ntouch.general;

import java.util.List;

public class Menu {

	private String id;
	private String text;
	private boolean leaf;
	//private String cls;
	private List<Menu> children;
	
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public boolean isLeaf() {
		return leaf;
	}
	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}
	public List<Menu> getChildren() {
		return children;
	}
	public void setChildren(List<Menu> children) {
		this.children = children;
	}
}


3、index.jsp中的部分Ext脚本代码
   /*树加载*/
			var root = new Ext.tree.AsyncTreeNode({
				id:'0',
				text:'区域',
				leaf:false
			});
			
			var data=new Ext.tree.TreeLoader({url:'zone.do?op=showTreeNode'});
			
			var tree = new Ext.tree.TreePanel({
				root:root,
				title:'结构组织',
				region:'west',
				split:true,//可调
				collapsible:true,//隐藏显示
				loader:data,
				useArrows:true,
				autoScroll:true,
				width:180,
				minSize:160,
				maxSize:300,
				animate:true,//开启动画效果
				enableDD:false,//不允许子节点拖动
				border:false,//有边框
				lines:true,
				rootVisible:true  //设为false,将隐藏root根节点
				
			});
			
			data.on('beforeload',function(treeLoader,node){
               this.baseParams.id=node.attributes.id;
            },data);
            
            root.expand(true,true);

你可能感兴趣的:(json,jsp,ext,脚本)