iterator - Iterating a dataProvider in flex -
i'm wondering... want iterate through dataprovider, in component based on dropdownlist. first thing, didn't work (it compiled, never iterated), was:
var o:object; each (var o:object in dataprovider) { }
i guess didn't work because ilist doesn't provide objects, or able explain easily.
i tried looks horrible efficiency perspective, nonetheless works. it:
for (var i:int = 0; < dataprovider.length; i++) { o = dataprovider.getitemat(i); }
but horrible felt tempted ask here possible solution.
update:
i'll try elaborate... i'm making (well, made already) component that, being dropdownlist, bindable, not index (like selectedindex="@{variable}"), value of variable inside of arraycollection.
say, have dataprovider 2 objects: {a:'5', nmb:'five', blabla:'cinco'}
, {a:'39', nmb:'thirty-nine', blabla:'treinta y nueve'}
.
this component, if declared this:
<com:ddlistn idxname="a" selectedn="@{val}" labelfield="nmb">
does use val
set/get dropdownlist proper index, comparing value variable defined in idxname.
well, entire code (is not 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 selectedn():string { return this.selecteditem[idxname]; } public function set selectedn(v:string):void { var o:object; // each (var o:object in dataprovider) @@ (var i:int = 0; < 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>
actually for each
loops little bit slower normal for
loops. take @ answer for vs foreach on array performance.
to answer question, why for each
loop doesn`t work. unfortunatly type of loop works types of classes. far know are:
array
vector
xml
xmllist
- and classes extend
proxy
implement functions neededfor each
loops.listcollectionview
, subclassesarraycollection
,xmllistcollection
ones know of.
collection classes arraylist
not work for each
loops since not native objects in flash player (like array
) , don't extend proxy
class.
so, best thing can use simple for
loops , loops faster for each
loops.
Comments
Post a Comment