Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

populating key values from arraycollection

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
royjayd
Asked:
royjayd
  • 2
  • 2
2 Solutions
 
royjaydAuthor Commented:
I am not sure if ArrayCollection is appropriate to store such key/value pairs
0
 
petiexCommented:
var frequency:ArrayCollection = new ArrayCollection(
                      [{data:1, label: "Weekly"},
                       {data:2, label:"Monthly"},
                       {data:3, label:"Yearly"}
                      ]
                        );
0
 
royjaydAuthor Commented:
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
 
dgofmanCommented:
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
 
petiexCommented:
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

Enhanced Intelligibility Without Cable Clutter

Challenge: The ESA office in Brussels wanted a reliable audio conference system for video conferences. Their requirement - No participant must be left out from the conference and the audio quality must not be compromised.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now