Sebastián
Sebastián

Reputation: 1

How can I get item label from selectOneMenu @ JSF page

I'm dealing with a wizard in primefaces like this one:

http://www.primefaces.org/showcase/ui/wizard.jsf

I would like to get the raw text from a selectOneMenu (from one of the tabs) and show it in the confirmation tab.

My selectOneMenu looks like this:

<p:selectOneMenu id="vinculos"
      value="#{socioAdicional.idVinculo}" required="true"
      label="Vinculo">
            <f:selectItem itemLabel="#{mensajes.combos_empty_txt}" itemValue="#{null}" />
            <f:selectItems value="#{controladorCombos.vinculos}"
                  var="vinculo" itemLabel="#{vinculo.descripcion}"
                  itemValue="#{vinculo.id}" />
</p:selectOneMenu>

All the values are obtained from a webservice and can't be stored in an map attribute, cause we are using other buffering strategies...

I've tried so far to print that text (on a different tab) like this:

<b>Vinculo: </b><h:outputText value="#{p:component('vinculos').getSelectedValue()}"/>

Any ideas?

Thanks!

Upvotes: 0

Views: 11130

Answers (1)

BalusC
BalusC

Reputation: 1108722

Either use a Map<ItemId, Item> as available items, so that you can get the whole Item at hands based on the selected item ID:

private Long selectedItemId;
private Map<Long, Item> availableItems;
<h:selectOneMenu value="#{bean.selectedItemId}">
    <f:selectItems value="#{bean.availableItems.values()}" var="item"
        itemValue="#{item.id}" itemLabel="#{item.description}" />
</h:selectOneMenu>
...
<b>Selected item:</b> #{bean.availableItems[bean.selectedItemId].description}.

Or use whole Item instead of Id as selected item, with a converter, so that you immediately already have the whole Item at hands:

private Item selectedItem;
private List<Item> availableItems;
<h:selectOneMenu value="#{bean.selectedItem}" converter="itemConverter">
    <f:selectItems value="#{bean.availableItems}" var="item"
        itemValue="#{item}" itemLabel="#{item.description}" />
</h:selectOneMenu>
...
<b>Selected item:</b> #{bean.selectedItem.description}.

Upvotes: 2

Related Questions