Learn how to a build a cloud-first strategyRegister Now

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

Coerce array of objects into selectedItems Array

I have a list in my mxml, and when I query the database I pull back an array of objects that should be hi lighted in the list. The trace in the function below prints one of the properties from the object fine, but it errors out on the next line.

When I trace on report.rsStat[x] as Array, it says null. How can I get each one of those objects into the selectedItems as an Array?

Or is there a better way to put all the objects in that array into the selectedItems so the proper items in the list are hi lighted?
if(report.rsStat.length > 0){
	var rssCursor:IViewCursor = this.rssCollection.createCursor();
	for (var x:String in report.rsStat)
	{
		
		if(rssCursor.findFirst(report.rsStat[x])){
			trace("VAR " + x + " " + report.rsStat[x].name);
			this.rsStat.selectedItems = report.rsStat[x] as Array; //ERROR here
			this.rsStat.dispatchEvent(new ListEvent(ListEvent.CHANGE));
		}
	}
}

Open in new window

0
hallikpapa
Asked:
hallikpapa
1 Solution
 
Jones911Commented:
report.rsStat is an array correct?  Its an array of objects.  So report.rsStat[x].name is one of the items you want selected?  You can cast a single item to an array.

Try this.
 if(rssCursor.findFirst(report.rsStat[x])){
                        trace("VAR " + x + " " + report.rsStat[x].name);
                        var temp:Array = new Array();
                        temp[0] = report.rsStat[x];                    
                        this.rsStat.selectedItems = temp;
                        this.rsStat.dispatchEvent(new ListEvent(ListEvent.CHANGE));
                }

Open in new window

0
 
hallikpapaAuthor Commented:
Well, that got rid of my error, but it isn't highlighting the items in the list that is now contained in rsStat.selectedItems.

What am I doing wrong?
<mx:Binding source="rsStat.selectedItems" destination="selectedRSStat" />
...
...
...
<mx:FormItem direction="horizontal" label="Location(s):" required="true" enabled="{editMode}">
	<mx:List id="rsStat" 
		width="200"
		dataProvider="{rsStatCollection}"
		enabled="{this.rs.selectedIndex > -1}"
		toolTip="Hold CTRL key to select multiple Location"
		labelField="name"
		allowMultipleSelection="true"/>
</mx:FormItem>

Open in new window

0

Featured Post

Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

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