flex datagrid进行删除或增加操作后自动刷新
源码:
<?xml version="1.0" encoding="utf-8"?>
<s:TitleWindow xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="668" height="326"
creationComplete="initComponent()"
title="指标计量单位"
fontSize="15"
close="closeWindow()">
<s:layout>
<s:VerticalLayout />
</s:layout>
<fx:Declarations>
<mx:StringValidator id="sval_1" source="{targetTypeName}" property="text"
tooShortError="字符串太短了,请输入最少4个字符. "
tooLongError="字符串太长了,请输入最长20个字符. "
minLength="4" maxLength="20"
trigger="{myButton}" triggerEvent="click"
/>
<mx:StringValidator id="sval_2" source="{targetCal}" property="text"
tooShortError="字符串太短了,请输入最少4个字符. "
tooLongError="字符串太长了,请输入最长20个字符. "
minLength="4" maxLength="20"
trigger="{myButton}" triggerEvent="click"
/>
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.adobe.serialization.json.JSON;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.CloseEvent;
import mx.events.ListEvent;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;
import mx.utils.URLUtil;
import mx.validators.Validator;
import org.osmf.utils.URL;
[Bindable]public static var mainApp:DataCenterTargetPreserve = null;
[Bindable]public var gsMyString:String;
[Bindable]public var gnMyNumber:Number;
[Bindable]public var acItemsSelected:Object;
[Bindable]
public var dataArray:ArrayCollection;
private function initComponent():void
{
var u:URLVariables=new URLVariables("temp="+Math.random());
var r:URLRequest=new URLRequest();
r.data=u;
r.method=URLRequestMethod.POST;
r.url=mainApp.urls+"/findTargetCal.do";
var l:URLLoader=new URLLoader();
l.load(r);
l.addEventListener(Event.COMPLETE, comp2);
}
function comp2(e:Event):void
{
var l:URLLoader=URLLoader(e.target);
var array:Array=JSON.decode(String(l.data)) as Array;
dataArray=new ArrayCollection(array);
myGrid.dataProvider=dataArray;
}
private function closeWindow():void
{
PopUpManager.removePopUp(this);
}//closeWindow
private function showFinalSelection(oEvent:Event):void
{
//mainApp.tiFinalSelection.text = oEvent.target.selectedItem.album;
}//showFinalSelection
protected function button1_clickHandler(event:MouseEvent):void
{
PopUpManager.removePopUp(this);
}
protected function button2_clickHandler(event:MouseEvent):void
{
var all:Array=Validator.validateAll([sval_1,sval_2]);
if(all.length==0){
var v:URLVariables=new URLVariables("targetTypeName="+targetTypeName.text+"&targetCal="+targetCal.text+"&temp="+Math.random());
var r:URLRequest=new URLRequest();
r.url=mainApp.urls+"/addTargetCal.do";
r.method=URLRequestMethod.POST;
r.data=v;
var l:URLLoader=new URLLoader();
l.load(r);
l.addEventListener(Event.COMPLETE,comp);
}else{
Alert.show("新增失败");
}
}
private function comp(e:Event):void{
var l:URLLoader = URLLoader(e.target);
var o:Object=URLUtil.stringToObject(l.data,";",true);
if(o.result=="add"){
Alert.show("新增成功");
var obj:Object=(JSON.decode(o.datas) as Object);
//dataArray.addItemAt(obj,0);
Alert.show(obj.targetCalId);
}
initComponent();
}
private function myClick(evt:CloseEvent):void{
//Alert.show("本次要删除的为"+mainDataGrid.selectedIndex);
if(evt.detail==Alert.YES){
//Alert.show(acItemsSelected.getItemAt(0).targetId);
var v:URLVariables = new URLVariables("id="+acItemsSelected.targetCalId+"&temp="+Math.random());
var r:URLRequest = new URLRequest();
r.url =mainApp.urls+"/deleteTargetCal.do" ;
r.method = URLRequestMethod.POST;
r.data=v;
var l:URLLoader = new URLLoader();
l.load(r);
//dataArray.removeItemAt(myGrid.selectedIndex);
l.addEventListener(Event.COMPLETE,comp1); //注册请求完成后 响应获取数据事件方法
}
}
private function comp1(e:Event):void
{
var l:URLLoader = URLLoader(e.target);
var o:Object = URLUtil.stringToObject(l.data, ";", true);
if(o.result=="delete"){
Alert.show("删除成功!","友情提示");
}
initComponent();
}
protected function button3_clickHandler(event:MouseEvent):void
{
if(myGrid.selectedItem==null){
Alert.show("请选择一项进行删除","友情提示");
}else{
var dlg_obj:Object=Alert.show("确认删除吗?", "友情提示!", Alert.YES|Alert.NO,null,myClick);
}
}
protected function button4_clickHandler(event:MouseEvent):void
{
if(myGrid.selectedItem==null){
Alert.show("请选择一项进行修订!","友情提示");
}else{
var v:URLVariables = new URLVariables("targetTypeName="+targetTypeName.text+"&targetCal="+targetCal.text+"&id="+acItemsSelected.targetCalId);
var r:URLRequest = new URLRequest();
r.url =mainApp.urls+"/updateTargetCal.do";
r.method = URLRequestMethod.POST;
r.data = v;
var l:URLLoader = new URLLoader();
l.load(r);
l.addEventListener(Event.COMPLETE,comp3); //注册请求完成后 响应获取数据事件方法
}
}
private function comp3(e:Event):void
{
var l:URLLoader = URLLoader(e.target);
var o:Object = URLUtil.stringToObject(l.data, ";", true);
if(o.result=="update"){
Alert.show("更新成功!","友情提示");
var obj:Object=(JSON.decode(o.datas) as Object);
//dataArray.setItemAt(obj,myGrid.selectedIndex);
}else{
Alert.show("操作失败!","友情提示");
}
initComponent();
}
protected function myGrid_itemClickHandler(event:ListEvent):void
{
acItemsSelected =myGrid.selectedItem;
}
]]>
</fx:Script>
<mx:Form width="651" height="282">
<mx:DataGrid width="616" height="160" id="myGrid" itemClick="myGrid_itemClickHandler(event)">
<mx:columns>
<mx:DataGridColumn headerText="计量名称" dataField="targetTypeName"/>
<mx:DataGridColumn headerText="计量符号" dataField="targetCal"/>
<mx:DataGridColumn visible="false" dataField="targetCalId" />
</mx:columns>
</mx:DataGrid>
<mx:HBox>
<s:Label width="33"/>
<mx:FormItem label="计量名称" >
<s:TextInput id="targetTypeName"/>
</mx:FormItem>
<mx:FormItem label="计量符号" >
<s:TextInput id="targetCal"/>
</mx:FormItem>
<mx:FormItem >
<s:Button label="新增" id="myButton" click="button2_clickHandler(event)" />
</mx:FormItem>
</mx:HBox>
<mx:FormItem >
</mx:FormItem>
<mx:HBox>
<s:Label width="250"/>
<mx:FormItem >
<s:Button label="删除" click="button3_clickHandler(event)"/>
</mx:FormItem>
<mx:FormItem >
<s:Button label="修改" click="button4_clickHandler(event)"/>
</mx:FormItem>
<mx:FormItem >
<s:Button label="刷新"/>
</mx:FormItem>
<mx:FormItem >
<s:Button label="关闭" click="button1_clickHandler(event)"/>
</mx:FormItem>
</mx:HBox>
</mx:Form>
</s:TitleWindow>