Flex: How to Reference What's Been Clicked When Using an ItemRenderer? - apache-flex

I've got a Tilelist and I'm using an ItemRenderer, which is a Button.
I don't understand how to reference what was clicked. So, if the first tile (which is the first Obect) is clicked, how do I know that that particular one was clicked.
<mx:ArrayCollection id="myAC">
<mx:Array>
<mx:Object id="first" label="1" />
<mx:Object label="2" />
<mx:Object label="3" />
<mx:Object label="4" />
</mx:Array>
</mx:ArrayCollection>
ItemRenderer:
<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml"
click="Alert.show( 'This was Clicked')">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
]]>
</mx:Script>
</mx:Button>
Thank you.
-Laxmidi

MouseEvent.target will give you the item was was clicked
If you want to access the element in your dataPRovider, you can use the data property in your itemRenderer. Something like this:
<mx:Button xmlns:mx="http://www.adobe.com/2006/mxml"
click="Alert.show( data['label'] + ' was Clicked')"

Related

Display different attributes of an object in different columns in data grid

Can anyone please tell me how to display different attributes of an object in different columns in datagrid in flex?
Have you tried to read the basic help page for flex DataGrid ?
http://livedocs.adobe.com/flex/3/html/help.html?content=dpcontrols_6.html
Here is a sample from the live docs:
<?xml version="1.0"?>
<!-- dpcontrols/DataGridVisibleColumn.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" >
<mx:DataGrid id="myDG" width="350">
<mx:dataProvider>
<mx:ArrayCollection>
<mx:source>
<mx:Object Artist="Pavement" Price="11.99"
Album="Slanted and Enchanted" />
<mx:Object Artist="Pavement"
Album="Brighten the Corners" Price="11.99" />
</mx:source>
</mx:ArrayCollection>
</mx:dataProvider>
<mx:columns>
<mx:DataGridColumn dataField="Artist" />
<mx:DataGridColumn dataField="Album" />
<mx:DataGridColumn id="price" dataField="Price" visible="false"/>
</mx:columns>
</mx:DataGrid>
<!-- The column id property specifies the column to show.-->
<mx:Button label="Toggle Price Column"
click="price.visible = !price.visible;" />
</mx:Application>

How to set tooltip for buttonbar button

I have a buttonbar that has 3 buttons. I'm trying to set a tooltip for each of the buttons, but not sure where. The ButtonBar itself has a skin, which in turn includes a skin for each of the buttons.
<s:ButtonBar id="bb" selectedIndex="0" skinClass="skins.bbSkin">
<s:dataProvider>
<s:ArrayList>
<fx:Object label="item1" />
<fx:Object label="item2" />
<fx:Object label="item3" />
</s:ArrayList>
</s:dataProvider>
</s:ButtonBar>
The skin for the buttonbar is skins.bbSkin
The skin inside it for each button is skins.bbbSkin
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:ButtonBar id="bb" selectedIndex="0" toolTipField="tooltip">
<mx:dataProvider>
<mx:Array>
<mx:Object label="item1" tooltip='hello1'/>
<mx:Object label="item2" tooltip='hello2'/>
<mx:Object label="item3" tooltip='hello3'/>
</mx:Array>
</mx:dataProvider>
</mx:ButtonBar>
</mx:Application>
If you want to display a toolTip with the spark ButtonBar you have to manage with the skinClass.
Have a look here :
https://sensaran.wordpress.com/2010/07/19/setting-tooltip-for-spark-buttonbar

TextArea being used as an itemEditor misbehaves when the enter key is pressed

I have a TextArea inside an itemEditor component, the problem is that when typing in the TextArea if the enter key is pressed the itemEditor resets itself rather moving the caret to the next line as expected:
<mx:List width="100%" editable="true" >
<mx:dataProvider>
<mx:ArrayCollection>
<mx:Array>
<mx:Object title="Stairway to Heaven" />
</mx:Array>
</mx:ArrayCollection>
</mx:dataProvider>
<mx:itemRenderer>
<mx:Component>
<mx:Text height="100" text="{data.title}"/>
</mx:Component>
</mx:itemRenderer>
<mx:itemEditor>
<mx:Component>
<mx:TextArea height="100" text="{data.title}"/>
</mx:Component>
</mx:itemEditor>
</mx:List>
</mx:Application>
Could anyone advise how I can get around this strange behaviour and make the enter key behave as expected?
Thanks,
Chris
The trick is to listen for the ITEM_EDIT_END event and prevent the default list behaviour if the reason is NEW_ROW. See example below:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="onComplete();">
<mx:Script><![CDATA[
import mx.events.ListEvent;
import mx.events.ListEventReason;
import mx.controls.TextArea;
private function onComplete():void
{
list.addEventListener(ListEvent.ITEM_EDIT_END, onEndEdit);
}
private function onEndEdit(e:ListEvent):void
{
if (e.reason == ListEventReason.NEW_ROW)
e.preventDefault();
else
list.editedItemRenderer.data.title = TextArea(e.currentTarget.itemEditorInstance).text;
}
]]></mx:Script>
<mx:List width="100%" editable="true" id="list">
<mx:dataProvider>
<mx:Object title="Stairway to Heaven" />
</mx:dataProvider>
<mx:itemRenderer>
<mx:Component>
<mx:Text height="100" text="{data.title}"/>
</mx:Component>
</mx:itemRenderer>
<mx:itemEditor>
<mx:Component>
<mx:TextArea height="100" text="{data.title}"/>
</mx:Component>
</mx:itemEditor>
</mx:List>
</mx:Application>
Looks like the keypress of [enter] is being handled by your lists default function rather than your item renderers. Not sure what the exact code to fix that is, but I would think you would need to extend the list control that would nix the function when the user presses [enter]
Think you can use the "editorUsesEnterKey" of List.as (line 544 Flex3.5)
A flag that indicates whether the item editor uses Enter key.

Flex/actionscript: Using dataDescriptor on an AdvancedDataGrid

I have a hierarchical data set that i would like to display in an advancedDataGrid. My data is a set of composed value objects that do not use the 'children' attribute. On a Tree control you can create a custom dataDescriptor to define which elements contain the children of the node. Can this be done on the datagrid as well?
Hierarchical data has a childrenField property that you can use to "tell" the control where should it look for branches.
http://livedocs.adobe.com/flex/3/html/help.html?content=advdatagrid_07.html
From Adobe live docs on AdvancedDataGrid examples, you can see that you can set the dataProvider of the AdvancedDataGrid to a GroupingCollection instance, in order to create some groups for your data. Going down the class hierarchy, there is HierarchicalData, which you need to use when setting your dataProvider on the AdvancedDataGrid. Example (sorry for using arrays instead of one XML):
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
<mx:Array id="dianaSerfes">
<mx:Object name="geen" surname="zod" />
</mx:Array>
<mx:Array id="xySerfs">
<mx:Object name="sdf" surname="sdfsdgd" />
</mx:Array>
<mx:Array id="johnSerfs">
<mx:Object name="jack" surname="the ripper" />
<mx:Object name="diana" surname="bloom" serfs="{dianaSerfes}"/>
</mx:Array>
<mx:Array id="myData">
<mx:Object name="xy" surname="zzz" serfs="{xySerfs}" />
<mx:Object name="blue" surname="zed" />
<mx:Object name="John" surname="smith" serfs="{johnSerfs}" />
</mx:Array>
<mx:AdvancedDataGrid width="100%" height="100%">
<mx:dataProvider>
<mx:HierarchicalData id="hd" source="{myData}" childrenField="serfs" />
</mx:dataProvider>
<mx:columns>
<mx:AdvancedDataGridColumn dataField="name" />
<mx:AdvancedDataGridColumn dataField="surname" />
</mx:columns>
</mx:AdvancedDataGrid>
</mx:Application>

Cannot get itemDoubleClick event to work in Flex (even with doubleClickEnabled=true)

I am trying to do a simple datagrid in Flex with a doubleclick event, but I cannot get itemDoubleClick to fire:
<mx:DataGrid id="gridReportConversions" height="100%" width="100%" mouseEnabled="true" doubleClickEnabled="true" itemDoubleClick="refererRowDoubleClicked(event)">
<mx:columns>
<mx:DataGridColumn width="75" dataField="qty" headerText="Qty" />
<mx:DataGridColumn dataField="referer" headerText="URL" />
</mx:columns>
</mx:DataGrid>
If I use the itemClicked event then the event is raised just fine. When I search for this problem I find many people saying 'you need to set doubleClickEnabled=true, but I've done that and it still doesn't work.
This control is nested within quite a few levels of VBox and other containers. Surely I dont need to set doubleClickEnabled on each of those containers do I?
Just to clarify how I tested this - I have an alert box in my refererRowDoubleClicked event handler and it never gets shown when I use itemDoubleClick
Simon,
I was able to get your code to work, no problem. Wrapped it up in several layers of containers that didn't have doubleClickEnabled set to true, to see if that was an issue, but it doesn't seem to be.
I'm wondering if one of the parents is causing a problem somehow. Would it be possible for you to post a larger section of the code?
Here is what I ran to test this with:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.collections.ArrayCollection;
[Bindable] private var dp:ArrayCollection = new ArrayCollection([{qty:1,referer:'http://google.com'},{qty:25,referer:'http://cnn.com'},{qty:4,referer:'http:stackoverflow.com'}]);
private function refererRowDoubleClicked(e:Event):void
{
var msg:String = "target: " + e.target + "\n\ncurrentTarget: " + e.currentTarget + "\n\nselected item qty: " + gridReportConversions.selectedItem.qty + "\nselected item referer: " + gridReportConversions.selectedItem.referer;
Alert.show(msg);
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:VBox width="100%" height="100%">
<mx:Box width="100%" height="100%">
<mx:Canvas width="100%" height="100%">
<mx:DataGrid id="gridReportConversions" height="100%" width="100%" dataProvider="{this.dp}"
mouseEnabled="true" doubleClickEnabled="true" itemDoubleClick="refererRowDoubleClicked(event)">
<mx:columns>
<mx:DataGridColumn width="75" dataField="qty" headerText="Qty" />
<mx:DataGridColumn dataField="referer" headerText="URL" />
</mx:columns>
</mx:DataGrid>
</mx:Canvas>
</mx:Box>
</mx:VBox>
</mx:VBox>
</mx:Application>
Before I use the propety doubleClickEnabled, my itemDoubleClick doesn't work, but when I set doubleClickEnabled=true, it work good, no problem.

Resources