<f: ajax> does not work on PrimeFaces component
I am trying to use the onChange selectOneMenu event, but it does not work and the component does not appear when I add the onChange attribute.
Can someone tell me how I can handle the onChange <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 the bean request scope:
public void serviceChange() { System.out.println("change"); } However, when I change the menu, nothing is printed.
How is this caused and how can I solve it?
First of all, onChange is an invalid event name. This is change . Second, if you intend to invoke the HTML attribute name, onChange also an invalid attribute name. This is onChange .
Returning to your specific problem; standard JSF <f:ajax> not compatible with PrimeFaces components. You should use PrimeFaces own <p:ajax> instead.
<p:selectOneMenu ...> ... <p:ajax listener="#{newOpProgramme.serviceChange()}" update="nomCdp" /> </p:selectOneMenu> Note that I omitted the event and process attributes. Both of them already have the correct default valueChange and @this respectively.
See also:
When I want to update something after a change in selectOneMenu, I use the <f:ajax> inside selectOneMenu as follows:
<h:selectOneMenu value="#{bean.selected}" > ... select items here <f:ajax event="change" execute="@this" render="search" /> </h:selectOneMenu> Where the search is the Id object you want to update.
Another solution is that you should try onchange not onchange .
<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 the identifier for <f:selectItems /> and set your choice on the bean support side by sending ajax itemValue (id).
Server-side method bean without 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..."); }