简体   繁体   中英

flex: drilling down into dataprovider arrays

I have an SQL Statement which I want to read the name fields from an SQL database. the code:

public function getAllGiberish():void {
  var stmt:SQLStatement = new SQLStatement();
  stmt.sqlConnection = sqlConnection;
  stmt.text = "SELECT name FROM test3";
  stmt.execute();
  l.dataProvider = new ArrayCollection(stmt.getResult().data);
}

This will pull the data from the db. However, in the list item it shows everything up as [object Object]. Upon debugging I can see that the data provider shows:

data[0] > name
data[1] > name
data[2] > name

Where the info that I want is the name within each data object.

How do I easily access this? It is a problem I keep coming across and would like to work it out once and for all!

Cheers

You want to set the labelField property on the list (Assuming Flex 3 here). By default it's looking for a field called "label" not "name". Also look at the dataField and labelFunction properties of the list object for some more advanced options.

You could write a label function:

private function list_labelFunction(item:Object):String {
            return item.columnName;
        }

And make sure that your List has a label function set:

<s:List id="myList" labelFunction="list_labelFunction"/>

That's how I did it anyway!

The result of the query returns an array of Object instances, while you might expect String, with their keys equal to the column names that you select. You will either need to alias the "name" column to "label" in your query (as the List control uses this as the default labelField as Chris metioned), or you need to set the labelField or a labelFunction on the List control.

Note that you can also return typed objects instead of plain Object instances by setting the itemClass property on the statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM