Problem setting text property of a combobox on application start in Flex
I am using the combocheck example from the following site: http://www.carballares.es/en/arcadio/?p=220
It's a very useful component and works very well.
Now, I need to set the text property of this combo box upon application startup with certain values (which are defined dynamically according to some criteria). The problem is that although I have no problem setting the text property of the combo box at other times of the application run, setting the property at startup seems to have no effect unfortunately.
For example, I want to set the text property of the combobox to "Bob" (which is one of the items in the dataprovider for the combobox) and the "set text" method is called o开发者_Python百科n the combo box. Performing a step-through shows that the "set text" method is receiving the correct value item but not setting the _text property.
I have tried not doing anything until "ApplicationComplete" but no joy there either.
Thanks in advance for any help or clues you can offer up.
//Edit Here is the code (a combination of the code from the URL above and some other changes to it). However, as I've stated above, the codes does work. The problem is that it doesn't work at the application startup. Whenever an event is fired and setComboText is called as a result, the text of the combo box is set correctly.
private function onComboChecked(event:ComboCheckEvent):void {
var obj:Object=event.obj;
var index:int=selectedItems.getItemIndex(obj);
if (index==-1) {
selectedItems.addItem(obj);
} else {
selectedItems.removeItemAt(index);
}
setComboText();
dispatchEvent(new Event("valueCommit"));
dispatchEvent(new Event("addItem"));
}
public function setComboText():void {
if (selectedItems.length>1) {
// text='multiple'
//modified by samuel
var items:ArrayCollection = selectedItems;
var selItems:String = "";
for (var item:String in items)
{
if (items[item].label != "ALL" ) {
selItems = selItems + items[item].label + ",";
}
}
if (selItems.length > 0){
selItems = selItems.substr(0,selItems.length - 1);
}
text = selItems;
}
if (selectedItems.length==1) {
text=selectedItems.getItemAt(0)[labelField];
}
if (selectedItems.length<1) {
//modified by samuel
text="ALL";
//Alert.show("text is "+text);
}
}
Try adding this method into the main ComboBox component you're extending :
public function set selectedValueList( val : String ) : void {
var selectedValueArray :Array = val.toLowerCase().split(',');
var dp :ArrayCollection = this.dataProvider as ArrayCollection;
var newSelectedItems:ArrayCollection=new ArrayCollection();
for each ( var selectedVal :String in selectedValueArray ){
for( var i :uint=0;i<dp.length;i++){
if( String(dp.getItemAt(i)[this.labelField]).toLowerCase() == selectedVal ){
newSelectedItems.addItem( dp.getItemAt(i) );
}
}
}
selectedItems = newSelectedItems;
if (selectedItems.length>1) { text='multiple'; }
if (selectedItems.length==1) { text=selectedItems.getItemAt(0)[labelField]; }
if (selectedItems.length<1) { text=''; }
dispatchEvent(new Event("valueCommit"));
dispatchEvent(new Event("addItem"));
}
Then you'll call it like this when you want to set the selected items :
myComboId.selectedValueList = 'Selected Label 1,Selected Label 2';
Or set it in the component mxml def:
<local:ComboBox selectedValueList="Selected Label 1,Selected Label 2" ...rest of properties.../>
Or bind to it :
<local:ComboBox selectedValueList="{variableHoldingCommaDelimList}" ...rest of properties.../>
Let me know how it goes. :)
I am not sure what you mean by "set text" in case of a combobox. Rather it should be selectedIndex that you should be setting. Apart from that, instead of applicationComplete OR creationComplete, override commitProperties method and assign this initial property there (althought you can simply assign selectedIndex in mxml itself).
精彩评论