简体   繁体   中英

How to display the label of a selected value of a <h:selectOneMenu> in a <h:outputText>?

I want to show the label of the selected value of <h:selectOneMenu /> in a <h:outputText /> .

I have the following dropdown where I get the selected value.

<h:column>
    <h:selectOneMenu value="#{bean.selectedCity}">
        <f:selectItem itemValue="1" itemLabel="NewYork"/>
        <f:selectItem itemValue="2" itemLabel="Washington"/>
    </h:selectOneMenu>
</h:column>

I want to display the selected value, but the following only shows 1 or 2 .

<h:outputText value="#{bean.selectedCity}" />

I want to display the label NewYork or Washington . How can I do this?

Update #2 based on the new edits and comments: ah we finally get somewhere (I removed the entire old answer, check edit history if you want to see it anyway).

You just need to maintain something like a Map<Long, String> cities somewhere in your model and then use it as follows:

<h:outputText value="#{bean.cities[bean.selectedCity]}" />

This will basically display bean.getCities().get(bean.getSelectedCity()); . You could even reuse the map for <f:selectItems> so that you don't need to maintain it in two places.

If you want to see its value, you need to use disable property and displayClass

you can use the t:selectOneMenu tag with diplayValueOnly="true" property

<h:selectOneMenu  disable="true">
        <f:selectItem id="si1" itemLabel="Thums Up" itemValue="11" />
</h:selectOneMenu>

<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>

 <t:selectOneMenu  displayValueOnly="true">
            <f:selectItem id="si1" itemLabel="Thums Up" itemValue="11" />
</t:selectOneMenu>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM