• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
If She Said "Yes".
What'll you do ?
博客园    首页    新随笔    联系   管理    订阅  订阅
Using a Delete button to a DataGrid to remove rows.

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.

View 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

posted on 2011-07-06 13:23  百花盛开  阅读(223)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3