Iterate dataProvider in flex

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> 
+4
source share
1 answer

In fact, for each loops are slightly slower than regular for loops. Take a look at the answer For VS Foreach on Array to work .

To answer your question why your for each loop is not working. Unfortunately, this type of loop only works for certain types of classes. As far as I know, this is:

  • Array
  • Vector
  • XML
  • XMLList
  • and Proxy extension classes that implement the functions needed for for each loops. ListCollectionView and its subclasses ArrayCollection and XMLListCollection are the only ones that I know of.

Collectible classes, such as ArrayList , do not work with for each loops, since they are not native objects in Flash Player (like Array ), and they do not extend the Proxy class.

So, the best thing you can do is use simple for loops, and these loops are even faster than for each loops.

+8
source

All Articles