Flex 2 Puzzle No 1: The Answer

Posted by Neil on June 07, 2006

OK, from the email I have received, a few of you have tried this, some with some success, and some without. The main problem people were having is related to the way of thinking that a traditional CF/HTML developer has. In HTML a select consists of values and labels:

bar

However, in Flex, this isn't the case, each item of a select (or combo to give it it's correct name) is an object. This essentially means you can chuck pretty much anything you want at the combo, you just need to tell it what part of the object is the label value. (Remember, I said this would be enlightening).

So if we take some mxml (which answers the question):

You will see a webservice being called when the application has finishing loading, this then returns a load of objects, which are in turn bound to the combo's dataprovider. To get a label simply point Flex at the item in the object that represents your string label (in this case CATEGORYNAME) using the labelField attribute. This will give you your Combo with your labels.

So how do we get at the data? Well, remembering these are objects in the combo, the data is right there in each items relevant object (so in theory you can have loads of data fields in one combo!). To demonstrate this we have a label bound to the selected items CATEGORYID.

So, now you know. If you are a HTML/CF developer this would have probably been quite hard for you to figure out, due to the fundamental way this is different to HTML (which is partly the point of this test being first, as it is such as big difference and had me stumped for ages). If you knew Flex already, it should have been a walk in the park.

Incidentally, does anyone have any feedback on these tests? Do you want me to publish any more?

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments