Using a Delete button to a DataGrid to remove rows.

本文将指导您如何在DataGrid中添加一个删除按钮,并实现点击删除按钮后相应行被移除的功能。

Problem

I'd like to have a Delete button in a DataGrid column, and when that button is clicked, that row of the DataGrid is removed.

Solution

In the Delete button click handler, access the DataGrid dataProvider and use the selectedIndex property to delete the item.

Detailed explanation

I'd like to have a Delete button in a DataGrid column, and when that button is clicked, that row of the DataGrid is removed.

In the Delete button click handler, access the DataGrid dataProvider and use the selectedIndex property to delete the item.

ContractedBlock.gifExpandedBlockStart.gifView Code
<?xml version="1.0" ?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.controls.DataGrid;
import mx.collections.XMLListCollection;

private var xml:XML =
<root>
<item>string one</item>
<item>string two</item>
<item>string three</item>
<item>string four</item>
<item>string five</item>
<item>string six</item>
</root>;

[Bindable]
private var xc:XMLListCollection = new
XMLListCollection(xml..item);

public function deleteItem(event:MouseEvent):void{
xc.removeItemAt(dg.selectedIndex);
xc.refresh();
}
]]
>
</mx:Script>
<mx:DataGrid id="dg" dataProvider="{xc}">
<mx:columns>
<mx:DataGridColumn headerText="String Text"
dataField
="item">
<mx:itemRenderer>
<mx:Component>
<mx:Text text="{data}"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
<mx:DataGridColumn headerText="Delete Item">
<mx:itemRenderer>
<mx:Component>
<mx:LinkButton label="Delete"
click
="outerDocument.deleteItem(event)"/>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>

note:

outerDocument keyword

we can use outerDocument keyword to acess the variable define outside the componet in an mxml

转载于:https://www.cnblogs.com/ifshe/archive/2011/07/06/2099113.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值