Wednesday 28 November 2012

Arrays of Objects



One of the most useful structures in Flash --especially when dealing with xml or database table data and list-type components like ComboBox and DataGrid-- is an array of objects. Because you can define any number of 'fields' as the properties of an object, and any number of 'records' in an array, the two make a good recordset-like combination.
The following code, for example, creates an array of objects that is sorted alphabetically by title and used as the data provider for a combobox component. The combobox (instancename bookchoices) displays a list of book titles. Each entry (element of the array) is associated with other data (the other properties of the object in that element) which could be used to populate an order form, saved in a database, or emailed to an interested party when selected from the dropdown by a site visitor:
var books:Array = [
        {title:"Flash book 1", author:"abc", pubyr:2001},
        {title:"Flash book 2", author:"def", pubyr:2000},
        {title:"Flash book 3", author:"ghi", pubyr:2001},
        {title:"Flash book 4", author:"Robert Penner", pubyr:2002}
        ];

books.sortOn("title");
bookchoices.labelField = "title";
bookchoices.dataProvider = books;

No comments:

Post a Comment