Solved

populating key values from arraycollection

Posted on 2011-09-13
5
310 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
  • 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 300 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 200 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
flash keyboard access html form 15 347
AS 3 Eval 5 490
[Q] XML to Object/Dictionary class available? 3 711
[HELP PLEASE] -- calc columns in datagrid 13 478
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…
Knowing where your website is hosted is as important as the features you receive, the monthly fee, and the support you receive. Due diligence should be done when choosing your next hosting provider.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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