0

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

2 Answers2

1

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.

Akos K
  • 7,071
  • 3
  • 33
  • 46
  • So that example omits the bean.java cuze its too simple or it is not needed!? – David Agustin Almanza Gaitan Nov 13 '12 at 20:29
  • @DavidAgustinAlmanzaGaitan correct, the backing in that example has two collections of the same type with appropriate getters/setter. I edited my answer. – Akos K Nov 13 '12 at 20:36
  • but how would that ajax method event be if I expect to receive some sort of selectedItem index for example? – David Agustin Almanza Gaitan Nov 13 '12 at 21:29
  • @DavidAgustinAlmanzaGaitan in that case `value="#{mbcompletado.selectedSectores}"` should point to the list of the selected indexes. – Akos K Nov 13 '12 at 21:41
  • @DavidAgustinAlmanzaGaitan have you succeed with detecting the selected items or you are still facing the same problem? – Akos K Nov 15 '12 at 17:54
  • yes, I can't get the selected item in the manymenu, yet. Still trying to solve this around! – David Agustin Almanza Gaitan Nov 16 '12 at 03:54
  • @DavidAgustinAlmanzaGaitan both answers are providing information about the selection. What do you mean by you can't get the selected item, is there any error message? Have you tried to set a breakpoint in the setter of `selectedSectores` list and debug your project? – Akos K Nov 16 '12 at 06:26
  • Ok I need a to feed a String list form a input txt but now I am using a collector to charge that list in to a gridTable, it's a component more practical and suitable. But it seems very odd that a manymenu does not handle the selected items and have a functionality like the itemSelectEvent from the chart components! – David Agustin Almanza Gaitan Nov 16 '12 at 21:09
  • @DavidAgustinAlmanzaGaitan this is false. the manyMenu can handle the selected item(s). whenever you select a new item in the manyMenu the setter of the `value` is called containing the list of the selected items in the manyMenu, also if you attach a `listener` on `p:ajax` it will be called whenever a selection happens. – Akos K Nov 16 '12 at 22:46
0

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555