I'm curious ... I want to iterate through the dataProvider in a component that is based on DropDownList. First, that it did not work (it compiled, but never repeated), was:
var o:Object; for each (var o:Object in dataProvider) { }
I suppose this did not work, because the IList does not provide objects or something that someone could easily explain.
I tried something that looks horrible in terms of efficiency, however it works. It is he:
for (var i:int = 0; i < dataProvider.length; i++) { o = dataProvider.getItemAt(i); }
But so terrible that I was tempted to ask here about another possible solution.
UPDATE:
I will try in more detail ... I am making (well, this has already been done) a component that, like DropDownList, is a binder, not an index (for example, selectedIndex = "@ {variable}"), but the value of the variable inside the ArrayCollection array.
Say you have a dataProvider with two objects: {a:'5', nmb:'five', blabla:'cinco'} and {a:'39', nmb:'thirty-nine', blabla:'treinta y nueve'} .
This component, if declared as follows:
<com:ddListN idxName="a" selectedN="@{val}" labelField="nmb">
Uses val to set / get DropDownList to the appropriate index, comparing the value with the variable defined in idxName.
Well, this is all the code (not so much):
<?xml version="1.0" encoding="utf-8"?> <s:DropDownList xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" change="ch()"> <fx:Declarations> </fx:Declarations> <fx:Script> <![CDATA[ private var _selectedN:String; public var idxName:String = 'n'; [Bindable(event="changeSelected")] public function get selectedN():String { return this.selectedItem[idxName]; } public function set selectedN(v:String):void { var o:Object; // for each (var o:Object in dataProvider) @@ for (var i:int = 0; i < this.dataProvider.length; i++) { o = dataProvider.getItemAt(i); if (o[idxName] == v) { this.selectedIndex = i; _selectedN = v; dispatchEvent(new Event("changeSelected")); return; } } this.selectedItem = null; // no seleccionar nada (@@?) _selectedN = null; dispatchEvent(new Event("changeSelected")); } private function ch():void { _selectedN = this.selectedItem[idxName]; dispatchEvent(new Event("changeSelected")); } ]]> </fx:Script> </s:DropDownList>