21.5. Populating Menu Components

Problem

You want to use a recordset to populate a menu component instance.

Solution

Use the setDataProvider( ) method for the menu component of interest. For more complex scenarios, use the DataGlue class.

Discussion

List boxes and combo boxes can both be populated by recordsets without much effort by employing one of two techniques. The first technique is to use the setDataProvider( ) method of the list box or combo box. You can pass a recordset reference to the setDataProvider( ) method, and it will know how to process it. The result is that the menu displays one item per record, with each item showing the record’s column values in a comma-delimited list. The order in which the column values appear within each item is determined by the order of the columns within the recordset column names array.

#include "NetServices.as"

rs = new RecordSet(["ID", "NAME"]);
rs.addItem({ID: 24, NAME: "a"});
rs.addItem({ID: 42, NAME: "b"});
rs.addItem({ID: 66, NAME: "c"});
rs.addItem({ID: 93, NAME: "d"});
rs.addItem({ID: 33, NAME: "e"});

// Populate a list box and a combo box with the values from the recordset.
myListBox.setDataProvider(rs);
myComboBox.setDataProvider(rs);

Additionally, when you use a recordset as a data provider for a menu, the object(s) returned by the getItemAt( ), getSelectedItem( ), and getSelectedItems( ) methods are record objects instead of the conventional objects with data and label properties. This also means that the getValue( ) method of ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.