删除AdvancedDataGrid中的行?

时间:2013-06-29 04:08:36

标签: actionscript-3 flex4

我有一个AdvancedDataGrid我想删除按钮点击的行。 我的代码是

[Bindable]
        private var sales:ArrayCollection = new ArrayCollection([
            {Sno:"1",categoryName:"biscuits", 
                description:"salt"}, 
            {Sno:"2",categoryName:"biscuits", 
                description:"cream"}, 
            {Sno:"3",categoryName:"sweets", 
                description:"less sweet"}, 
            {Sno:"4",categoryName:"sweets", 
                description:"normal sweet"}, 
            {Sno:"5",categoryName:"fruits", 
                description:"healthy "}, 
            {Sno:"6",categoryName:"fruits", 
                description:"sweetfruits"}]);
private function deleteRows():void{

            sales.removeItemAt(myADG1.selectedIndex);
        }

我的网格是

<mx:AdvancedDataGrid id="myADG1" width="100%" height="100%" contentBackgroundColor="#F7F0F0"
                     initialize="gc.refresh();" variableRowHeight="true"  >        
    <mx:dataProvider>
        <mx:GroupingCollection2 id="gc" source="{sales}">
            <mx:grouping>
            <mx:Grouping label="Sno">
                <mx:GroupingField name="categoryName"/>
            </mx:Grouping>
            </mx:grouping>
        </mx:GroupingCollection2>
    </mx:dataProvider>        

    <mx:columns>
        <mx:AdvancedDataGridColumn width="125" dataField="Sno" headerText="Sno"/>
        <mx:AdvancedDataGridColumn width="175" dataField="categoryName" headerText="CategoryName"/>
        <mx:AdvancedDataGridColumn width="150" dataField="description" headerText="categoryDescription"/>

    </mx:columns>
</mx:AdvancedDataGrid>

销售是我的datatprovider。上面的代码没有删除选定的行。任何人都可以帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

由于您使用的是分组集合,因此您必须从网格中获取selectedItem,并在原始ArrayCollection中查找其索引:

private function deleteRows():void {
    var index:int = sales.getItemIndex(myADG1.selectedItem);
    if (index != -1) {
        sales.removeItemAt(index);
    }
}