PDA

View Full Version : Radio Button passing object help?


justmeadam
01-09-2007, 12:51 AM
Hey everyone, I am having a bit of trouble and for all I know I am making something too complicated.

I have a repeater that runs through an array where each object is a date and number.

The repeater works fine, but what I would LOVE to happen is when someone selects one of the radio buttons the "date and number" from that object / selected index gets passed to a column chart.

Ideally the chart would have one date / number and the 2nd date / number would come come from the radio buttons, this would be used for comparing two objects. One is static and that gets compared with the one you selected.

For now I have gotten rid of the chart and am just trying to pass the two items to a datagrid, I have gone through so many changes I am losing my mind ... not much to lose I know but any help would be appreciated!

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[

import mx.collections.ArrayCollection;

private var arrayData:Array=[
{date:"12-24-06", avg:"22"},
{date:"12-27-06", avg:"65"},
{date:"12-28-06", avg:"11"},
{date:"01-07-07", avg:"89"}];

[Bindable]
private var dp:ArrayCollection=new ArrayCollection(arrayData);

[Bindable]
public function displayData(repeaterItem : Object):void
{

averageLabel.text=repeaterItem.avg;
dateLabel.text=repeaterItem.date;
trace (repeaterItem.date);


}

]]>
</mx:Script>

<mx:Label id="averageLabel" text="Average"/><mx:Label id="dateLabel" text="Date"/>

<mx:Repeater id="myRepeater" dataProvider="{dp}">
<mx:RadioButton label="{myRepeater.currentItem.date}"
click="displayData(event.target.getRepeaterItem())" />
</mx:Repeater>


<mx:DataGrid id="test" dataProvider="{myRepeater.currentIndex}">
<mx:columns>
<mx:DataGridColumn headerText="Column 1" dataField="itemRepeater.date"/>
<mx:DataGridColumn headerText="Column 2" dataField="itemRepeater.avg"/>
</mx:columns>
</mx:DataGrid>



</mx:Application>

dr_zeus
01-09-2007, 06:33 PM
This should give you what you need. I added a RadioButtonGroup to give you a central location to access the selected item from the RadioButtons.

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[

import mx.collections.ArrayCollection;

private var arrayData:Array = [
{date:"12-24-06", avg:"22"},
{date:"12-27-06", avg:"65"},
{date:"12-28-06", avg:"11"},
{date:"01-07-07", avg:"89"}];

[Bindable]
private var dp:ArrayCollection = new ArrayCollection(arrayData);

public function displayData(repeaterItem : Object):void
{
averageLabel.text=repeaterItem.avg;
dateLabel.text=repeaterItem.date;
trace (repeaterItem.date);
}

]]>
</mx:Script>

<mx:Label id="averageLabel" text="Average"/><mx:Label id="dateLabel" text="Date"/>

<!-- Use a RadioButtonGroup -->
<mx:RadioButtonGroup id="repeaterGroup"/>

<mx:Repeater id="myRepeater" dataProvider="{dp}">

<!-- Notice that you need to specify the groupName -->
<mx:RadioButton groupName="repeaterGroup" label="{myRepeater.currentItem.date}" click="displayData(event.target.getRepeaterItem())" />

</mx:Repeater>

<!-- Bind to the RadioButtonGroup's selectedValue property -->
<mx:DataGrid id="test" dataProvider="{repeaterGroup.selectedValue}">
<mx:columns>
<!-- Notice that the dataField is a string specifying the property in the object to access -->
<mx:DataGridColumn headerText="Column 1" dataField="date"/>
<mx:DataGridColumn headerText="Column 2" dataField="avg"/>
</mx:columns>
</mx:DataGrid>

</mx:Application>

justmeadam
01-09-2007, 07:38 PM
Hey Z thanks for that, for some reason both columns are displaying the date even though one of them is clearly "avg". Grrr @ flex ;)

I see so what is happening is it is actually passing the value of the label of the selected item in the group and not actually passing the "date" and "avg" data.

dr_zeus
01-09-2007, 09:28 PM
You're right, change this line:

<mx:RadioButton groupName="repeaterGroup" value="{myRepeater.currentItem}" label="{myRepeater.currentItem.date}" click="displayData(event.target.getRepeaterItem())" />

justmeadam
01-09-2007, 10:54 PM
You're right, change this line:

<mx:RadioButton groupName="repeaterGroup" value="{myRepeater.currentItem}" label="{myRepeater.currentItem.date}" click="displayData(event.target.getRepeaterItem())" />

I came to the exact same result, thanks for the help this is huge for me, there are some functions you would think would be more drag and drop that just take that little bit extra, thanks again Z