Another puzzle down, and again, a whole load of good answers and feedback. Hopefully some of you are learning something from this.
The answers I am using today are from Kevin Hoyt as they provide the clearest examples of the way to do things. They are also the closest to what I had. This code can be downloaded in a zip file from here.
Ok, lets break this down into two parts. The “Please Select” prompt, and the formatting of the select items.
1) Please select
Some of you spotted the very easy way of doing this, i.e via the mx:ComboBox “prompt” attribute. This gives you the ability to provide a string as the selected element until another item is selected. At this point, the Prompt value is removed.
So what if you want to have the item showing all the time, i.e you want to add an element that has meaning, but is not returned from the webservice? Well, this is where we need to treat the combo as what it really is, an array of objects. By manipulating this array, we can perform pretty much anything we want on it. We can reformat values, we can add or remove values etc etc. If you take a look at the source of squirrelFixed.mxml you will notice a function doPrompt(). This function creates a new object, then sticks it into element 0 of the categories arrayCollection. Due to the bindings, this essentially has the same effect as adding it to the combo.
So why the arrayCollection? Well, these come in handy for quite a few things, mainly being able to manipulate some data without having to do anything fancy such as reinvoking a webservice. Try to think of an array collection as an array of structures in CF.
2) Reformatting the options
Easy one this, that a lot of people made extra complicated for themselves. The key? The labelFunction attribute of mx:ComboBox. This little gem lets Flex redirect an object to a given function that then returns a string containing the label. The benefit of this is that you do not need to pre-process this label, or save it off anywhere in another object. You simply process the labels when you need to see them. Cool huh? For an example, take a look at squirrelPrompt.mxml and categoryMerge(). Notice that this is only talking in the context of the single object passed in (which happens automagically).
So in summary, I hope that this has pointed out that Flex usually provides a neat simple way of doing most things, you just need to look through the API's. And secondly, that if something doesn't quite fit, you can take it on yourself and do it your own way.
Stay tuned next week for the next test.











