From fb3836d1040da767cad56eee6195704bda506d71 Mon Sep 17 00:00:00 2001 From: Bruno Garcia Date: Sat, 13 Dec 2008 00:14:01 +0000 Subject: [PATCH] get/setSelectedValue for modifying the index based on the data of an option. git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@740 ed5b42cb-e716-0410-a449-f6a68f950b19 --- src/as/com/threerings/flex/CommandComboBox.as | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/as/com/threerings/flex/CommandComboBox.as b/src/as/com/threerings/flex/CommandComboBox.as index 1233daca..6bca0422 100644 --- a/src/as/com/threerings/flex/CommandComboBox.as +++ b/src/as/com/threerings/flex/CommandComboBox.as @@ -72,11 +72,38 @@ public class CommandComboBox extends ComboBox _cmdOrFn = fn; } + /** + * Handy function to lookup an option based by argName and set it as the selected item. + * This probably only works for Array dataProviders. + */ + public function setSelectedValue (value :Object) :void + { + for (var ii :int = 0; ii < dataProvider.length; ++ii) { + var v :Object = (this.argName != null) ? + dataProvider[ii][this.argName] : dataProvider[ii]; + if (v == value) { + this.selectedIndex = ii; + return; + } + } + + // Assign to the prompt + this.selectedIndex = -1; + } + + /** + * The value that will be passed to the command or function based on argName and the + * current selected item. + */ + public function getSelectedValue () :Object + { + return (this.argName != null) ? this.selectedItem[this.argName] : this.selectedItem; + } + protected function handleChange (event :ListEvent) :void { - if (this.selectedItem != null) { - CommandEvent.dispatch(this, _cmdOrFn, - (this.argName != null) ? this.selectedItem[this.argName] : this.selectedItem); + if (this.selectedIndex != -1) { + CommandEvent.dispatch(this, _cmdOrFn, getSelectedValue()); } }