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

隐身守侯 提交于 2020-01-03 05:01:08

问题


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?


回答1:


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.




回答2:


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>


来源:https://stackoverflow.com/questions/4801776/how-to-display-the-label-of-a-selected-value-of-a-hselectonemenu-in-a-houtp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!