Select Item from many menu

Ok I have a simple many menu in wich I call a listener

<p:selectManyMenu style="width: 100%;" id="cmbsectores" valueChangeListener="#{mbcompletado.removeItem}">                                            
      <f:selectItems value="#{mbcompletado.sectores}"/>    
      <f:ajax update="@this"/>
</p:selectManyMenu>

I am looking the way I can use the ValueChangeEvent pass as parameter to detect which item was selected?? So I can use my business logic!

Do i need to use ajax tag? I found an itemSelect event in primeface, framework which I am using, but it only works on charts components!

Thanks in advance

Upvotes: 0

Views: 3562

Answers (2)

BalusC
BalusC

Reputation: 1108742

You need the <f:ajax listener> (or in this case better <p:ajax listener>) method instead. The ValueChangeListener serves an entirely different purpose and should only be used when you're really interested in both the old and new value, not when you're only interested in the new value.

E.g.

<p:selectManyMenu value="#{bean.selectedSectors>
    <f:selectItems value="#{bean.availableSectors}"/>    
    <p:ajax listener="#{bean.selectedSectorsChanged}" />
</p:selectManyMenu>

with

private List<String> selectedSectors;
private List<String> availableSectors;

public void selectedSectorsChanged() {
    System.out.println("Selected sectors are: " + selectedSectors); // Look, JSF has already set it.
    // ...
}

See also:

Upvotes: 0

Akos K
Akos K

Reputation: 7133

Since you are already using PrimeFaces use p:ajax instead of f:ajax. The event is already set to the appropriate event (valueChanged).

To detect the selected values of the selectManyMenu the value attribute is necessary:

<p:selectManyMenu style="width: 100%;" id="cmbsectores"
    value="#{mbcompletado.selectedSectores}">                                            
    <f:selectItems value="#{mbcompletado.sectores}"/>    
    <p:ajax/>
</p:selectManyMenu>

You can remove the valueChangeListener listener altogether.

For a more complete example see SelectManyMenu.

EDIT:

In your backing bean mbcompletado.selectedSectores should point to a collection of the same type like your mbcompletado.sectores. For example, if your sectores is a List of TypeA, selectedSectores should be also a List of the same type (TypeA).

Similar backing-bean structure can be found in the following example SelectManyCheckbox.

Upvotes: 1

Related Questions