I have an HtmlSelectOneMenu on a form and have a binding to that object in the backing bean. What I need to do is get the value of the label for the SelectItem that is selected. There is a getValue() method but that only gets the value. So for example if I had
<option value="0">Dot Net Sucks</option>
getValue() returns "0". I need to get Dot Net Sucks. Anyone know how to go about doing this?
The "UISelectItems" type of object can hold a map type of value, where in: The keys of the map object (once converted to Strings) are assumed to be labels, and the values of this object (once converted to Strings) are assumed to be values, of SelectItem instances that will be constructed dynamically and added to the set of available options for the parent component, in the order provided by an iterator over the keys.
Now your class can have a map object for generating the dynamic drop-down and the SelectOneMenu object. The SelectOneMenu will return you the selected value, which you can use in the map object for finding out the label.
I havent used this so not sure if this will work actually.
I'm sure that will work. The problem I have then is I guess with the JSF implementation as it doesn't support a getLabel() method on the SelectOneMenu component. It doesn't make sense to have left that out. ASP.NET allows you to do this.
I even tried to use getChildren on the SelectOneMenu which gives me the UISelectItems but when I try and get UISelectItems children, which should objects of type SelectItem I get a 0 count on the number of children. But even if that worked, it was still way too much code.
Joined: May 30, 2002
Just curious, why do you require a label ? (unless your value-label combination forms a composite primary key in database, which seems highly unlikely since labels, I believe, are just more user friendly version of values)
Originally posted by K Varun: Just curious, why do you require a label ? (unless your value-label combination forms a composite primary key in database, which seems highly unlikely since labels, I believe, are just more user friendly version of values)
I am providing a confirmation page for chosen form items. So when the user clicks Save they are presented the same form except the selectOneMenu's are replaced with outputText items and I need the Label to display to the user. The values are all numbers. And yes, I am using a value-label combo in my tables where the value is an int and the label is a string. The int's are stored as a foreign key in another table so that if I ever needed to change the label of an item I won't have to go and update thousands of entries in all different tables.
Joined: May 30, 2002
subject: JSF: HtmlSelectOneMenu and getting the values