views:

622

answers:

1

I'll show you the function first.

private var areaCollection:ArrayCollection;

private function generateAreaCollection():void
{
    areaCollection = new ArrayCollection();    
    areaCollection.addItem({Areal: "Totalareal:", Verdi: int(totalArea * 100) / 100 + " kvm"});
    areaCollection.addItem({Areal: "Hovedtakets areal:", Verdi: int(result.area* 100) / 100 + " kvm"});

    //Lots of other stuff omitted (just more addItems).
}

As you see, the order i put the items in the ArrayCollection is Areal, then Verdi (area, value)

When i loop through the collection later, the order changes. Now it is Verdi then Areal (value, area).

Does anyone have an idea of what might be the problem?

It really ruins things when I pass it over to a PHP-script for table-making in html later. (I have several different dynamic DataGrids that differs in size and "values", so I can't really point directly to f.ex "Areal" in my PHP-script)

And by the way, does anyone know how i can remove that pesky mx_internal_uid?

+4  A: 

Raw objects in AS3 (Object class, or things defined by {} ), have no sorting. That is, the order is unpredictable. You're confusing two ideas here I think. The Areal and Verdi strings are keys within an object map. The array collection is a list composed of two such objects. So any sorting applied to the array collection will sort those objects, but not within the object.

So you need to refer to areaCollection.source[0].Areal to get "Totalareal". And areaCollection.source[1].Verdi to get int(result.area* 100) / 100 + " kvm"

If you do for(var s:String in areaCollection.source[0]) { } you will iterate twice, with the value of "s" being "Areal" and "Verdi" or vice-versa (e.g, order not guaranteed).

Make sense? If you need order, you can make the object an array instead ["Totalareal", (int(totalArea * 100) / 100 + " kvm")], and then access "Verdi" using [1].

P.s. not sure how to remove the mx_internal_id.

Glenn
I understand.Another question then: How can I use the ArrayCollection as a dataprovider for a DataGrid (Flex 3) when the array is not associative? Is it possible to access array[0] in dataField?
oletk
My experience with DataGrid is limited, but I think your original approach is correct. You want an ArrayCollection of associative objects. Since you know the columns order, maybe you want to create a separate static map of columns to indexes before you send them to your PHP script? e.g., MAP = {"Areal": 0, "Verdi":1}, and then write a serialization function that turns those objects into arrays. var phpArray:Array = [];for each(var o:Object in arrayCollection) { var tmpA:Array = []; for (var s:String in o) { tmpA[MAP[s]] = o[s]; } phpArray.push[tmpA];}
Glenn