Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

populating key values from arraycollection

Posted on 2011-09-13
5
Medium Priority
?
323 Views
Last Modified: 2012-05-12
hi guys

I have a requirment where i have a arraylist

public var frequency:ArrayCollection = new ArrayCollection();

I want to store these values in the ArrayCollection

1, weekly
2, monthly
3, Yearly

On the screen I am displaying dropdown with the frequency ArrayCollection like this

private function createColumn(columnIndex:int,fieldObject:Object):AdvancedDataGridColumn{
var column:AdvancedDataGridColumn = new AdvancedDataGridColumn();
var factory:ClassFactory;
if(fieldObject.hasOwnProperty("cellType")){            
column.editable = true;
if(fieldObject.cellType=="combo"){
factory  = new ClassFactory(ComboBox);
factory.properties= {dataProvider:frequency}; //display frequency ArrayCollection here
column.itemEditor = factory;                              
}
}

In the dropdown i want weekly, monthly or Yearly to be displayed but when user submits the screen it should submit 1,2,3 to the backend.


Any idea how i can do this?

thanks

0
Comment
Question by:royjayd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Author Comment

by:royjayd
ID: 36532578
I am not sure if ArrayCollection is appropriate to store such key/value pairs
0
 
LVL 11

Accepted Solution

by:
petiex earned 1200 total points
ID: 36532790
var frequency:ArrayCollection = new ArrayCollection(
                      [{data:1, label: "Weekly"},
                       {data:2, label:"Monthly"},
                       {data:3, label:"Yearly"}
                      ]
                        );
0
 

Author Comment

by:royjayd
ID: 36532920
thanks. in the drop down i see Weekly, Monthly and Yearly
but how can i  set the value of 'data' in the frequency:ArrayCollection

i want to set the value of 'data'  to 1 , 2 or 3

thanks
0
 
LVL 29

Assisted Solution

by:dgofman
dgofman earned 800 total points
ID: 36533083
You need to use labelFunction and in this function you can customize you label output.

<mx:ComboBox labelFunction="calendarLabelFunction" ...

private function calendarLabelFunction(item:Object):String{
   return item.label + " (" + item.data + ")";
}
0
 
LVL 11

Expert Comment

by:petiex
ID: 36539056
Here's something that works:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx" width="100%" height="100%" creationComplete="myinit()">
    <fx:Script><![CDATA[
        import mx.collections.ArrayCollection;
        import mx.controls.Alert;
        import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
        import mx.controls.ComboBox;

        [Bindable]
        public var dataProv:Array = [
            {name:"Helen", frequency:1},
            {name:"Fred", frequency:2},
            {name:"Alphonse",frequency:3}
        ];
        public var frequency:ArrayCollection = new ArrayCollection([
            {data:0, label:""},
            {data:1, label:"Weekly"},
            {data:2, label:"Monthly"},
            {data:3, label:"Yearly"}
        ]);

        private function myinit():void {
            var dataFields:Array = [
                {columnHeader:"Frequency", cellType:"combo", dataField:"frequency"},
                {columnHeader:"Name", dataField:"name"}
            ];

            var idx:int = 0;
            var cols:Array = [];
            for each (var obj:Object in dataFields) {
                cols.push(createColumn(idx++, obj));
            }
            datagrid.columns = cols;
            datagrid.dataProvider = dataProv;
        }

        private function showNewData():void {
            var newData:String = "";
            for each (var obj:Object in dataProv){
                newData += "name: "+obj.name +", frequency: "+obj.frequency+"; ";
            }
            Alert.show(newData);
        }

        private function createColumn(columnIndex:int, fieldObject:Object):AdvancedDataGridColumn {
            var column:AdvancedDataGridColumn = new AdvancedDataGridColumn();
            column.dataField = fieldObject.dataField;
            column.headerText = fieldObject.columnHeader;
            if (fieldObject.cellType) {
                var factory:ClassFactory;
                column.editable = true;
                if (fieldObject.cellType == "combo") {
                    factory = new ClassFactory(ComboBox);
                    factory.properties = {dataProvider:frequency};
                    column.itemEditor = factory;
                    column.editorDataField = "selectedIndex";
                    column.labelFunction = function(obj:Object, dgc:AdvancedDataGridColumn):String {
                        return frequency[obj[dgc.dataField]].label
                    };
                }
            }
            else column.editable = false;
            return column;
        }


        ]]></fx:Script>
    <mx:AdvancedDataGrid id="datagrid" editable="true">
        <mx:columns/>
    </mx:AdvancedDataGrid>
    <mx:Button click="showNewData()" label="Show new data"/>
</mx:Application>

Open in new window

0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question