<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx=" http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.profiler.showRedrawRegions;
import flexunit.utils.ArrayList;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.List;
import mx.events.DragEvent;
import mx.events.FlexEvent;
[Bindable]
public var lix:ArrayCollection =new ArrayCollection([{name:"1",phone:"456",email:"789"},{name:"2",phone:"2",email:"3"}]);
protected function berfor():void {
trace(dg.dataProvider.length);
var item:Object = new Object();
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
ay.addItemAt(item,dg.selectedIndex);
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
protected function affter():void{
var item:Object = new Object();
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
ay.addItemAt(item,dg.selectedIndex+1);
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
protected function dele():void{
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
var index:int =dg.selectedIndex;
dg.dataProvider.removeItemAt(index);
index=dg.selectedIndex-1;
dg.dataProvider.refresh();
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
]]>
</fx:Script>
<s:Panel x="0" y="10" width="100%" height="100%">
<s:Button x="128" y="12" label="在之前插入" enabled="{dg.selectedIndex ==-1?false:true}" click="berfor()"/>
<s:Button x="287" y="12" label="在之后插入" enabled="{dg.selectedIndex ==-1?false:true}" click="affter()"/>
<s:Button x="445" y="12" label="删除" enabled="{dg.selectedIndex ==-1?false:true}" click="dele()"/>
<mx:DataGrid id="dg" x="34" y="41" width="581" height="321" dataProvider="{ lix }"
rowCount="5" editable="true">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="序号" />
<mx:DataGridColumn dataField="phone" headerText="X坐标"/>
<mx:DataGridColumn dataField="email" headerText="Y坐标"/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:Application>
<s:Application xmlns:fx=" http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.profiler.showRedrawRegions;
import flexunit.utils.ArrayList;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.List;
import mx.events.DragEvent;
import mx.events.FlexEvent;
[Bindable]
public var lix:ArrayCollection =new ArrayCollection([{name:"1",phone:"456",email:"789"},{name:"2",phone:"2",email:"3"}]);
protected function berfor():void {
trace(dg.dataProvider.length);
var item:Object = new Object();
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
ay.addItemAt(item,dg.selectedIndex);
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
protected function affter():void{
var item:Object = new Object();
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
ay.addItemAt(item,dg.selectedIndex+1);
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
protected function dele():void{
var ay:ArrayCollection=dg.dataProvider as ArrayCollection;
var index:int =dg.selectedIndex;
dg.dataProvider.removeItemAt(index);
index=dg.selectedIndex-1;
dg.dataProvider.refresh();
for(var i:int;i<ay.length;i++){
ay.getItemAt(i).name=i+1;
}
}
]]>
</fx:Script>
<s:Panel x="0" y="10" width="100%" height="100%">
<s:Button x="128" y="12" label="在之前插入" enabled="{dg.selectedIndex ==-1?false:true}" click="berfor()"/>
<s:Button x="287" y="12" label="在之后插入" enabled="{dg.selectedIndex ==-1?false:true}" click="affter()"/>
<s:Button x="445" y="12" label="删除" enabled="{dg.selectedIndex ==-1?false:true}" click="dele()"/>
<mx:DataGrid id="dg" x="34" y="41" width="581" height="321" dataProvider="{ lix }"
rowCount="5" editable="true">
<mx:columns>
<mx:DataGridColumn dataField="name" headerText="序号" />
<mx:DataGridColumn dataField="phone" headerText="X坐标"/>
<mx:DataGridColumn dataField="email" headerText="Y坐标"/>
</mx:columns>
</mx:DataGrid>
</s:Panel>
</s:Application>