问题
i am using below code for using linkbutton in flex datagrid
<mx:DataGridColumn headerText="Case ID" width="80">
<mx:itemRenderer>
<fx:Component>
<mx:Canvas>
<mx:LinkButton id="lnkCaseId" click="outerDocument.lnkCaseIdClick(event)" label="{data.caseId}" textDecoration="underline" color="#0052A5">
</mx:LinkButton>
</mx:Canvas>
</fx:Component>
</mx:itemRenderer>
</mx:DataGridColumn><br/>
now on link button click i want linkbutton label name and selected row inside lnkCaseIdClick method, how can i do this?
thanks.
回答1:
Here is sample according to explanation in @J_A_X but its using default MouseEvent You can extent *MouseEvent* Class to hold your custom data
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
click="{clicked(event)}">
<mx:Script>
<![CDATA[
import mx.controls.LinkButton;
import mx.core.UIComponent;
import mx.controls.Alert;
public function clicked(event:MouseEvent):void
{
if (event.target is LinkButton)
{
var innerLinkButon:LinkButton = event.target as LinkButton;
Alert.show("Application : "+innerLinkButon.label);
}
}
]]>
</mx:Script>
<mx:DataGrid id="grid">
<mx:dataProvider>
<mx:ArrayCollection>
<mx:Array>
<mx:Object label="AAAA"/>
<mx:Object label="BBBB"/>
<mx:Object label="CCCC"/>
<mx:Object label="DDDD"/>
</mx:Array>
</mx:ArrayCollection>
</mx:dataProvider>
<mx:columns>
<mx:DataGridColumn id="columnA" headerText="columnA" dataField="@label">
<mx:itemRenderer>
<mx:Component>
<mx:LinkButton click="{clicked(event)}" label="{data.label.toString()}">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function clicked(event:MouseEvent):void
{
Alert.show("linkButton");
}
]]>
</mx:Script>
</mx:LinkButton>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>
Hopes that helps
回答2:
Don't use outerDocument.lnkCaseIdClick(event)
, it's a horrible practice since you're assuming the function will always be there and makes your code coupled.
You should look into bubbling a custom event that holds the data you need from the item renderer, and then from your container, add the event listener for your custom event.
来源:https://stackoverflow.com/questions/5978176/flex-get-linkbutton-label-name-and-selected-row-of-datagrid