开发者

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.selectedC开发者_如何学编程ity}">
        <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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜