I need to make a ComboBox extension someday that uses this and binds in some way. It seems pretty useful in many situations, but it’s not quite generic enough yet.
The basic functionality is this…. take an existing ComboBox that uses a dataprovider – preferably with objects of some sort, pass in a parameter that exists in the object, such as a primary key name, pass in a value that you want to match to the object parameter and finally, pass in a data object that will be pushed into the ComboBox if no value is found.
This makes sure that something is always set for a ComboBox, even if the object you expect to be set is not found for some reason.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// sets and returns the index in a combo box for a selected field and value // if value is not found, the data object will be pushed into the combo box private function setComboBoxIndex(cb:ComboBox, field:String, value:*, data:*):int{ var result:int = -1; if (value > -1){ var idx:int = 0; for (idx=0; idx < cb.dataProvider.length; idx++){ if (cb.dataProvider[idx][field] == value){ cb.selectedIndex = idx; return idx; } } cb.dataProvider.addItemAt(data,0); setComboBoxIndex(cb, field, value, data); } return result; } |