- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" minWidth="955" minHeight="600">
- <mx:Script>
- <![CDATA[
- import mx.collections.ArrayCollection;
- import mx.collections.IList;
- [Bindable]
- private var arrayCollection:ArrayCollection=new ArrayCollection([
- {id:"1",name:"小1"},
- {id:"2",name:"小2"},
- {id:"3",name:"小3"}
- ]);
- public function moveUp():void{
- var i : int = dataGrid1.selectedIndex;
- if (i >= 1&&dataGrid1.selectedItem){
- IList(dataGrid1.dataProvider).addItemAt(dataGrid1.selectedItem,i-1);
- IList(dataGrid1.dataProvider).removeItemAt(i+1);
- dataGrid1.selectedIndex = i;
- }
- }
- public function moveDown():void{
- var i : int = dataGrid1.selectedIndex;
- if (i < (ArrayCollection(dataGrid1.dataProvider).length - 1) && dataGrid1.selectedItem){
- IList(dataGrid1.dataProvider).addItemAt(dataGrid1.selectedItem,i + 2);
- IList(dataGrid1.dataProvider).removeItemAt(i);
- dataGrid1.selectedIndex = i;
- }
- }
- private function addColumnItem():void{
- arrayCollection.addItem({id:"id",name:"name"});
- dataGrid1.selectedIndex=arrayCollection.length;
- }
- private function delColunmItem(colunmItem:Object):void{
- arrayCollection.removeItemAt(arrayCollection.getItemIndex(colunmItem));
- dataGrid1.selectedIndex=arrayCollection.length-1;
- }
- ]]>
- </mx:Script>
- <mx:Panel width="500" height="300" title="DataGrid简单测试">
- <mx:HBox width="100%" height="100%">
- <mx:DataGrid width="100%" height="100%" id="dataGrid1" dataProvider="{arrayCollection}">
- <mx:columns>
- <mx:DataGridColumn dataField="id" headerText="ID"/>
- <mx:DataGridColumn dataField="name" headerText="姓名"/>
- </mx:columns>
- </mx:DataGrid>
- <mx:VBox width="60">
- <mx:Spacer height="100%"/>
- <mx:Button label="上移" id="up" click="moveUp()" enabled="{dataGrid1.selectedItem?true:false}"/>
- <mx:Spacer height="5"/>
- <mx:Button label="下移" id="down" click="moveDown()" enabled="{dataGrid1.selectedItem?true:false}"/>
- <mx:Spacer height="5"/>
- <mx:Button label="╂" click="addColumnItem()"/>
- <mx:Spacer height="5"/>
- <mx:Button label="━" enabled="{dataGrid1.selectedItem?true:false}" click="delColunmItem(dataGrid1.selectedItem)"/>
- <mx:Spacer height="100%"/>
- </mx:VBox>
- </mx:HBox>
- </mx:Panel>
- </mx:Application>
移动的代码参考地址: