Dynamic RadioButtons - apache-flex

Our resident Flex expert is out for the day and I thought this would be a good chance to test this site out. I have a dropdown with a dataProvider that is working fine:
<ep:ComboBox id="dead_reason" selectedValue="{_data.dead_reason}"
dataProvider="{_data.staticData.dead_reason}"
labelField="#label" width="300"/>
The ComboBox is custom but I'm not sure if that matters for the question. I need to change the combo box to radios (all in one group) but maintain the dynamic options. In other words, what is the best way to generate dynamic RadioButtons?

Try using an <mx:Repeater> Something like:
<mx:Repeater dataProvider="{_data.staticData.dead_reason}">
<mx:RadioButton groupName="reasons" ...>
</mx:Repeater>

<mx:RadioButtonGroup id="RDO_Group"/>
<mx:Repeater id="myRepeater" dataProvider="{_data.staticData.dead_reason}">
<mx:RadioButton id="rdo" label="{myRepeater.currentItem}" value="{myRepeater.currentItem}" groupName="RDO_Group"/>
</mx:Repeater>
is the best way.

Related

Datagrid for flex mobile

I've seen many posts on using datagrid for flex mobile apps and the performance issues that comes with it.
I wish i could get those performance issues because my problem is that i can't even see the datagrid component in my component list, only list, datagroup e.t.c
I have tried to uncheck the "Only show recemmended component" but i still cant find it in the component list. But when i create a flex project (not mobile), i see it. But then, i want to use it in a mobile project.
This will be my first mobile app. Any help will be appreciated.
Thanks in advance.
How about write below code? Of course, you can add component at source view also. Not only can it by using design view and component list.
<s:DataGrid id="grid" width="100%" height="100%">
<s:columns>
<s:ArrayList>
<s:GridColumn dataField="name" headerText="Name"/>
<s:GridColumn dataField="age" headerText="Age"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
As far as I remember, it can use DataGrid in mobile project without any special settings.

Group Box component in Flex

Is there a group box in Flex?
Is there any other control with same property?
I tried this link, but it has no answers. Also google
did not help me in this case.
there is a spark form element which you can use directly and you can include headings there.
As copied from the source http://help.adobe.com/en_US/flex/using/WSc5cd04c102ae3e973827a8e912ac2b325a4-8000.html
<s:Form id="myForm" width="450" height="125">
<s:FormHeading label="Spark Form Heading" />
<s:FormItem label="Username:">
<s:TextInput id="username" />
<s:helpContent>
<s:Label text="Enter your LDAP username" />
</s:helpContent>
</s:FormItem>
</s:Form>
If you just want to group elements you need to look at custom components (if they provide a source you can just modify those) like
https://www.assembla.com/code/shinylib/subversion/nodes
http://jayachandrababu.blogspot.de/2010/01/groupbox-fieldset-legend-component.html
I hope that helps
Andreas
Skinning a panel may also help. You can use panel's title as header like #Jason Sturges' provided link. Customizing panel's layout will also provide you with the group box you wanted.

Flex Forms: getting labels to line up, right-justified

I thought this was the default behaviour (all the Adobe docs seem to indicate that this is the case).
Say you have a form:
Name: [______]
Password: [________]
generally you want Name and Password to line up on their right-hand side (right justification). The length should be the length of the longest label.
When I add the following code to my MXML (authoring in Flex 4) it does not do that at all! But rather tjust crams everything left-justified, similar to the Name/Password example above.
What's the solution?
<s:Form id="directoryForm_A" width="100%">
<s:layout>
<s:VerticalLayout horizontalAlign="justify"/>
</s:layout>
<s:FormItem label="Click">
<s:Button label="Button"/>
</s:FormItem>
<s:FormItem label="Root Directory">
<s:TextInput x="0" width="100%" enter="handleUserSetRootDirectory(event)"/>
</s:FormItem>
</s:Form>
Which Adobe docs were you reading? You realize that the Spark Form are going to be very different than the Halo / MX Form. I would also suspect that the Spark form is not working yet.
Here are the docs on the Spark Form.
I think you'll benefit from reading the layout rules section of the FormItemLayout details. I cannot find the documentation that claims that all labels will be right aligned.
For anyone looking to a beta solution, Peter deHaan posts about this exact topic here: http://blog.flexexamples.com/2010/08/28/setting-the-text-alignment-on-a-spark-formitem-label-in-flex-hero/
Do note, since his example doesn't translate directly over to my issue:
adding to the for some reason disables the ability for the renderer to calculate the maxLabelWidth
Hope this helps others in the future.

Recreating/resetting views with Flex ViewStack

I'm writing an Adobe AIR application using a ViewStack for the different application states. Is there a way to make sure that each view component is created/destroyed each time it is shown/hidden?
For instance, if I have a TextInput in a view, I want it to reset to its initial state each time I change to that view, rather than having previously entered text. Or, if I have a Timer, I want it to be destroyed when I leave the view so that it doesn't keep running when I'm in an unrelated part of the application. I know that I can manually initialize/destroy everything on the show() and hide() events, but is there an easier way?
AFAIK there is no built-in way to do this, so you'll have to do it manually by handling the show and hide events as you mention.
ViewStack does however have two methods "saveState" and "loadState" which could perhaps help you out with this. The history manager uses these methods to enable back/forward navigation. The docs don't seem to have any examples though.
ViewStacks can be the work of the devil when it comes to creation/deletion policies and managing state. We had all sorts of problems when we developed fiat ecoDrive and by about 3/4 of the way though we we're all very anti ViewStacks for the management of view state within our application.
However... a good bet would be to first set the creationPolicy to ContainerCreationPolicy.NONE. That way it's in your control as to when to create any of the panels in your ViewStack. Then i would think you would need to have some sort of logic so that as the ViewStack changes a panel it deletes or resets the one you were on.
Another viable alternative would be to use view states instead. Have a base state which acts as the main container and then a simple state for each of your sections. That way when you switch to a new state, the old state gets removed in reverse order to the way it was created. You do have to be disciplined with states though as they can end up getting really complex and messy when they start becoming nested. If you keep it simple it may work as you require.
In MXML 2009, you can use itemDestructionPolicy="auto" to destroy a component after use it. If you use this property in the first view component with two states (init, logged), you can destroy and reinitialize all child view components. Example :
<s:states>
<s:State name="init" />
<s:State name="logged" />
</s:states>
<s:SkinnableContainer id="skincon" width="100%" height="100%" backgroundAlpha="0"
backgroundColor="#FFFFFF">
<s:VGroup id="MainContainer" width="100%" height="100%" paddingTop="0"
paddingLeft="20" paddingRight="20" gap="0">
<views:_HeaderView id="header" />
<mx:ViewStack id="viewStack" width="100%" height="100%">
<s:NavigatorContent includeIn="init" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
</s:layout>
<views:LoginView title="Login" id="loginView" />
</s:NavigatorContent>
<s:NavigatorContent includeIn="logged" itemDestructionPolicy="auto">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="top" />
</s:layout>
<views:_CentralView id="userView" />
</s:NavigatorContent>
</mx:ViewStack>
<views:_FooterView id="footer" />
</s:VGroup>
</s:SkinnableContainer>
Both answers are correct -- there doesn't seem to be any built-in way to do it. I solved the problem by creating a "wrapper" component for my view component. It creates a new view component each time the view is shown. This isn't ideal, but fits my requirements nicely, and required few changes to my application structure.
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" show="init()" hide="cleanup()">
<mx:Script>
<![CDATA[
private var myComponent:MyComponent;
private function init():void
{
myComponent = new MyComponent();
componentContainer.addChild(myComponent);
}
private function cleanup():void
{
componentContainer.removeAllChildren();
}
]]>
</mx:Script>
<mx:Canvas width="100%" height="100%" id="componentContainer" />
</mx:Canvas>
Build your "views" as separate Modules, and then use the ViewStack to switch between them. You could then write a function to destroy the unused module(s) (check each module against the selectedChild property) when the ViewStack's "change" event is fired.
2ยข
I am using different states for my different views. On each state change i add and remove components.
This causes the add and remove events of UIComponent fire which allows me to initialize and cleanup my components each time they are added.
This is the idea...
<mx:states>
<mx:State name="state1">
<mx:AddChild>
<mx:SomeUIComponent id="myComp" add="myComp.initialize()" remove="myComp.cleanup()"/>
</mx:AddChild>
</mx:State>
</mx:states>

Flex Help: Repeaters not repeating in an Accordian control

I am having a issue with databinding child repeaters inside an accordion control, hopefully you can help...
I have an accordion in a ViewStack (of which,that ViewStack is also in another top-level ViewStack). I have a repeater in each child of the accordion control. The component looks like such:
<mx:Box
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()"
>
<mx:ViewStack&gt
...
<mx:Accordion creationComplete="accordianInit()">
<mx:Box label="Groups" width="100%">
<mx:Repeater id="rpGroups" width="100%">
<mx:CheckBox id="chkGroups"
label="{rpGroups.currentItem.name}" />
</mx:Repeater>
</mx:Box>
<mx:Box label="Contacts">
<mx:Repeater id="rpContacts">
<mx:CheckBox id="chkContacts"
label=quot;{rpContacts.currentItem.full_name}" />
</mx:Repeater>
</mx:Box>
</mx:Accordion>
...
</mx:ViewStack&gt
<mx:Box>
The problem is that if I bind the 2 repeaters in the init function, then both repeaters don't show any data. If I bind the repeaters in the accordianInit function, then only the first repeater (rpGroups) gets databound...
Where should I be data binding the repeaters so that both repeaters repeat properly?
Hopefully this makes sense, if not I can elaborate more, any help is appreciated.
Bind the dataProvider of the repeater to the source in MXML itself:
<mx:Repeater dataProvider="{the_data}" ... />
The reason you're seeing the behavior you are is because the Accordion (and ViewStack) does not create all of it's children right away. It only creates the child that is visible (so, the first Box, and the first ViewStack child initially).
Because of this behavior, when you try to assign data to the repeaters in the first init() event handler the repeaters have no instantiated container to repeat children into. When you assign data to the repeaters in accordionInit() then only the first Box has been created, which is why only the first repeater works.
If you don't want to bind to the data via the dataProvider attribute of the Repeater tag (as I've shown above), then you can use a change handler on the Accordion to set the repeater data as the user changes panes (because as the user clicks into the panes, they get created by the Flex framework).
All of this comes about from the creationPolicy property: http://livedocs.adobe.com/flex/3/html/layoutperformance_05.html

Resources