View Full Version : dynamically generate menu items
arun vallappan
08-07-2007, 02:00 PM
i want my menu to dynamically generate submenu items .... I have values stored in an array.. how can i get these array values to be shown as submenu items
regards
drkstr
08-07-2007, 06:13 PM
Bind a var of type ArrayCollection to your sub menu then assign the array to this var like this:
[Bindable]
private var myMenuData:ArrayCollection;
<mx:MenuBar dataProvider="{myMenuData}" creationComplete="myMenuData = new ArrayCollection( your_array )" />
Regards,
...aaron
arun vallappan
08-07-2007, 08:19 PM
sorry i made a mistake ... its not a menubar that i want .... i want those as subitems in my popup .... sorry for the trouble
<mx:XML format="e4x" id="popUpItems" >
<root>
<menuitem label="View Profile"/>
<menuitem label="Edit Profile"/>
<menuitem label="Draw Link"/>
<menuitem label="Show Neighbours"/>
<menuitem label="Delete Node"/>
</root>
</mx:XML>
now if i want subitems for one of the items and try using an arraylist and bind it it comes it one single line .... i want one item in one line ... is this clear
regards
drkstr
08-07-2007, 11:04 PM
A sub menu uses a standard hierarchal data provider.
For Objects
var arrayCollection:ArrayCollection = new ArrayCollection();
var topMenu:Object = new Object();
var subMenu:Object = new Object();
topMenu.label = "Top Menu";
topMenu.children = new ArrayCollection();
subMenu.label = "Sub Menu";
topMenu.children.addItem(subMenu);
For XML
<mx:XML format="e4x" id="popUpItems" >
<root>
<menuitem label="View Profile"/>
<menuitem label="Edit Profile"/>
<menuitem label="Draw Link">
<menuitem label="Sub Menu"/>
</menuitem>
<menuitem label="Show Neighbours"/>
<menuitem label="Delete Node"/>
</root>
</mx:XML>
Are you wanting to mix items in an array to a data provider in XML? If this is the case, you will just have to write some code to parse it piece by piece and assemble the data provider. I wouldn't recommend that from a design perspective though.
Hope this helps,
...aaron
arun vallappan
08-08-2007, 08:34 AM
aaron now for draw link you have hardcoded the value Sub menu ... what if i want that to be dynamic is that possible.... now i tried to bind values from an array but it comes in a single line .... it should be one below the other ... how do i do it
regards and thanks a lot for your help
selva
drkstr
08-08-2007, 03:09 PM
Use a bindable ArrayCollection instead of an Array or the display object won't update. You can add and remove elements dynamically.
Best regards,
...aaron
arun vallappan
08-09-2007, 10:45 AM
but the real problem comes with displaying these items one in a line as in one time i would want to display 2 items one below the other and the next time may be 3 .... the dataprovider is not the problem ... it is stored as arrayname[0],arrayname[1] etc but how do i display them ... i just cant use the name of the array as everything gets displayed in one line ....
i cant use <menuitem label ="{arayname[0]}"/>
<menuitem label ="{arrayname[1]}"/>
as the size of the array varies each time
so it would be great if you could tell me how to proceed with this
thanks a lot
drkstr
08-09-2007, 10:48 PM
I already told you. Refer to my previous example. Here is a link that explains it in more detail.
http://livedocs.adobe.com/flex/201/html/about_dataproviders_057_13.html
For properties that have sub menus, you create a 'children' property which is an also ArrayCollection of menu item objects.
If you set up a hierarchal data structure like I showed you in my example, then assigned it as the data provider to your menu, you can add and remove items any where in the structure dynamically.
Read the link. If you're still having problems after that, then we'll talk.
Best regards,
...aaron
arun vallappan
08-14-2007, 07:43 PM
in that example should i write arrayCollection.addItem(topmenu)
and if use arrayCollection as a data binder it displays object object and not its contents
what should i do for that
sorry for the trouble
regards
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.