Solved

populating key values from arraycollection

Posted on 2011-09-13
5
306 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now