6

I trying to use the onChange event of selectOneMenu, but it doesn't work and the component is not displayed when I add onChange attribue.

Can someone tell me how can I handle the onChange event of <p:selectOneMenu>?

Here is my view:

<p:selectOneMenu id="service" filterMatchMode="startsWith">  
    <f:selectItem itemLabel="Selectionner un Service : "  />  
    <f:selectItems value="#{newOpProgramme.listeSevice}" var="service" itemValue="#{service.serviceId}" itemLabel="#{service.serviceNom}"/>
    <f:ajax event="change" execute="@this" listener="#{newOpProgramme.serviceChange()}" render="nomCdp"/>
</p:selectOneMenu>

And here is the <f:ajax listener> method in a request scoped bean:

public void serviceChange() {
    System.out.println("change");
}

When I change the menu, however, nothing is been printed.

How is this caused and how can I solve it?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
ThunderPhoenix
  • 1,534
  • 4
  • 18
  • 43
  • 1. Post your code; 2. You have to use the attribute `onchange`. `selectOneMenu` does not have the attribute `onChange`. – user1983983 Aug 20 '13 at 08:28

3 Answers3

26

First of all, onChange is the wrong event name. It's change. Secondly, if you intend to call the HTML attribute name, onChange is also the wrong attribute name. It's onchange.

Coming back to your concrete problem; the standard JSF <f:ajax> is not compatible with PrimeFaces components. You should be using PrimeFaces own <p:ajax> instead.

<p:selectOneMenu ...>
    ...
    <p:ajax listener="#{newOpProgramme.serviceChange()}" update="nomCdp" />
</p:selectOneMenu>

Note that I omitted the event and the process attributes. They both have already the right default value of valueChange and @this respectively.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
1

When I want to update something after change in selectOneMenu, I use <f:ajax> tag inside selectOneMenu like this:

  <h:selectOneMenu value="#{bean.selected}" >
... select items here
     <f:ajax event="change" execute="@this" render="search" />
  </h:selectOneMenu>

Where search is the Id of the object you want to update.

Other solution is that you should try onchange not onChange.

ThunderPhoenix
  • 1,534
  • 4
  • 18
  • 43
-1
<p:selectOneMenu value="#{someBean.myAttr.id}" valueChangeListener="#someBean.mySelectioMethodListener}">
    <f:selectItems value="#{someBean.listAttrs}" var="item"
        itemLabel="#{item.name}" itemValue="#{item.id}" />
    <p:ajax process="@this" update="someElementId" />
</p:selectOneMenu>

You must put an Id for <f:selectItems /> and set your selection on backing bean side by posted ajax itemValue (id).

Server side method bean without a converter:

public void mySelectionMethodListener(ValueChangeEvent event) {
ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());        
    SomeBeanDao someBeanDao = (SomeBeanDao) context.getBean(SomeBeanDao.class);
    myAttr = someBeanDao.byId((Long) event.getNewValue());
    System.out.println("value changed...");
}
ThunderPhoenix
  • 1,534
  • 4
  • 18
  • 43
andrexterz
  • 17
  • 3