flex TreeGrid使用

 flex端

一,首先引入 trees  xmlns:trees="http://code.google.com/p/flexlib/"

二 定义变量  private var reslutList:ArrayList=new ArrayList();
			
							private var timer:Timer = new Timer(300,1);
							[Bindable]
							private var data_list:Array = null;
							
三、定义方法:
   //定时器
   protected function onTimer(event:TimerEvent):void{
				openItem();
			}
	//展开树
			protected function openItem():void
			{
				for(var i:int=0; i<data_list.length; i++)
				{
					if(!data_list[i].leaf)//如果还有下一级
					{
						detail_tree.openItemAt(i);
					}
				}
			}
			
			/**
			 * 取出所有节点的数据
			 * */
			private function getListByGrid(list:Array):void{
				
				for(var i:int=0;i<list.length;i++){
					var o:Object=list[i];
					if(o.hasOwnProperty("leaf")){
						reslutList.addItem(o);
					}else{
						getListByGrid(o.children);
					}
				}
			}
			
			//加载数据
			protected function load_grid(year:String):void
			{
				// TODO Auto-generated method stub
				var param:URLVariables = new URLVariables();
				param.orgCd = HttpUtil.loginOrgCd;
				param.businessYear =year;// this.year;
				HttpUtil.doPost("bfProjectBudgetAction!getBfProjectBudget.do", param, function(message:Object):void{
					if(message.data.list!=null){
						detail_tree.dataProvider = message.data.tree;
						data_list=message.data.list;
						timer.start();
					}
				});
			}
			
			//保存方法
			protected function saveBtn_clickHandler(event:MouseEvent):void
			{
				reslutList=new ArrayList();
				var list:ArrayCollection=detail_tree.dataProvider as ArrayCollection;
				getListByGrid(list.toArray());
				var param:URLVariables = new URLVariables();
				var json:String=JSON.encode(reslutList.toArray());
				param.json=json;
				HttpUtil.doPost("bfProjectBudgetAction!updateBfProjectBudgetSubsidyDemo.do",param,function(obj:Object):void{
					if(obj.data.error){
						Message.showError("保持失败!");
					}else{
						Message.showSuccess("保存成功!");
					}
				});
			}
四、定义表格:
<trees:TreeGrid
				headerBackgroundSkin="skin.grid.MxGridHeaderSkin"
				borderColor ="#C8CCCE"
				id="detail_tree" showRoot="false" editable="true"
				width="100%" height="100%" sortableColumns="false" 
				disclosureClosedIcon="@Embed(source='/images/treegrid/arrow_down.png')"
				disclosureOpenIcon="@Embed(source='/images/treegrid/arrow_down.png')"
				folderOpenIcon="@Embed(source='/images/treegrid/arrow_down.png')"
				folderClosedIcon="@Embed(source='/images/treegrid/arrow_down.png')"
				defaultLeafIcon="@Embed(source='/images/treegrid/arrow_down.png')" 
				>
				<trees:columns>
					<trees:TreeGridColumn id="projectTypeName" headerText="项目类型" dataField="projectTypeName" editable="false" textAlign="left"/>
					<mx:DataGridColumn id="gcl" headerText="计划工程量" dataField="projectQty"  textAlign="center" editable="false" />
					<mx:DataGridColumn id="unitName" headerText="单位" dataField="unitName" textAlign="center" editable="false"/>
					<mx:DataGridColumn  headerText="* 国家补贴(元)" dataField="countrySubsidy"  textAlign="center" editable="true" >
						<mx:itemEditor>
							<fx:Component>
								<mx:TextInput  restrict="0-9."  visible="{data.leaf}" editable="{data.leaf}"  width="100%" height="100%"/>
							</fx:Component>
						</mx:itemEditor> 
					</mx:DataGridColumn>
					<mx:DataGridColumn  headerText="budgetId" dataField="budgetId" visible="false"/>
				</trees:columns>
			</trees:TreeGrid>
			


java端

	一、取数据方法
			
	  newList.add(0, new SMap("projectTypeCd", "0", "projectTypeName", "项目类型", "projectParentId", null));
		Map tree = TreeUtil.getTree(newList, "0", "projectTypeCd", "projectParentId");
		newList.remove(0);
		Map result = new HashMap();
		result.put("tree", tree);
		result.put("list", newList);
		this.output(result)
		
		****取数据时一定要排序,因为这个问题今天查了很久才查出来,这种问题真的很要命。。
		
		二 保存数据
		
		  Map outPut=new HashMap();
    	String json = getReq().getParameter("json");
    	List list = (List) JSON.parseArray(json, Map.class);
    	try{
    		bfProjectBudgetService.updateBfProjectBudgetFlagDemo(shType,list);
    		outPut.put("error", false);
    	}catch(Exception e){
    		outPut.put("error", true);
    		outPut.put("errorString", "保存失败");
    	}
    	this.output(outPut);
		


TreeUtil需要引进pipi-framework.jar

你可能感兴趣的:(flex TreeGrid使用)