Subham
Subham

Reputation: 503

selectonemenu is not updating from another selectonemenu using<p:ajax > primefaces

I am trying to update selectonemenu by selecting some value from other selectonemenu.i am using<p:ajax update=":main:deviceosName" listener="#deviceRegistration.updateOSName}" /> But the listener is not calling updateOSName method . here i am trying to update selectonemenu with id -deviceosName after selecting some value from selectonemenu with id-manufectureName

one another thing selectonemenu with id-manufectureName is updating again.

here is my xhtml page-

<h:form id="main" enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />

    <p:panel id="os_version_panel" header="Os Version">
        <h:panelGrid columns="3" title="Os Version" style="width:40%;height:20%;padding:10%;padding-left:30%">

            <h:outputLabel for="manufectureName" value="Manufecture Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:selectOneMenu id="manufectureName" value="#{deviceRegistration.selectedManufacture}" effect="fade" converter="deviceManufactureConverter"
                style="width:200px">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{deviceRegistration.manufactureNameList}" var="manufacturer" itemLabel="#{manufacturer.manufacturersName}"
                    itemValue="#{manufacturer}" />
                <p:ajax event="valueChange" update=":main:deviceosName" listener="#{deviceRegistration.updateOSName}" />
            </p:selectOneMenu>

            <h:outputLabel for="deviceosName" value="Device OS Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:selectOneMenu id="deviceosName" value="#{deviceRegistration.selectedOsName}" effect="fade" converter="deviceOsNameConverter"
                style="width:200px">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{deviceRegistration.osNamesList}" var="name" itemLabel="#{name.osName}" itemValue="#{name}" />
            </p:selectOneMenu>

            <h:outputLabel for="versionName" value="Device OS Version Name:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:inputText id="versionName" value="#{deviceRegistration.selectedVersion.osVersionName}" required="true" label="versionName" size="30">
                <f:validateLength minimum="2" />
            </p:inputText>
            <h:outputLabel for="version" value="Device Os Version:*" style="float:right;font-size:15px;width:200px" />
            <p:spacer width="20" height="50" />
            <p:inputText id="version" value="#{deviceRegistration.selectedVersion.osVersion}" required="true" label="version" size="30">
                <f:validateLength minimum="2" />
            </p:inputText>
            <f:facet name="footer">
                <p:spacer width="20" height="80" />

                <p:commandButton id="addOsVersion" value="Add OS Version" actionListener="#{deviceRegistration.addOsVersion}"
                    action="addversion.xhtml?faces-redirect=true" ajax="false" style="float:right;" />
            </f:facet>
        </h:panelGrid>
    </p:panel>


</h:form>

Upvotes: 0

Views: 3695

Answers (3)

BalusC
BalusC

Reputation: 1108722

I am trying to update selectonemenu by selecting some value from other selectonemenu. But the listener is not calling updateOSName method.

That can happen if a conversion or validation error has occurred. The INVOKE_APPLICATION phase will then simply be skipped altogether. You're not using <p:growl autoUpdate="true"> and likely also not paying attention to warnings in server logs and hence those converison/validation errors have escaped your attention.

Make sure that you pay attention to conversion/validation errors. The chance is namely big that you actually have a

main:manufectureName: Validation Error: Value is not valid

If this is true, then head to the following answer for all possible causes and appropriate solutions: Validation Error: Value is not valid

Another possible cause is that you don't have the PrimeFaces FileUploadFilter properly registered. You've there namely a <h:form enctype="multipart/form-data"> which is by default not supported by JSF. However, this would not have resulted in only this problem, rather the whole form would not be submittable/processable at all and this does not seem to be the case based on the information provided so far.

Upvotes: 1

Tapas Bose
Tapas Bose

Reputation: 29806

A sample application, which is working fine. The managed bean:

package app.so.dev.web.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.AjaxBehaviorEvent;

import app.so.dev.web.model.Manufacturer;
import app.so.dev.web.model.Product;

@ManagedBean(name = "so15369641")
@ViewScoped
public class SO15369641 implements Serializable {

    private static final long serialVersionUID = 3128349336575959334L;

    private static final String[] manufacturerNames;
    private static final String[] productNames;
    private List<Manufacturer> manufacturers;
    private List<Product> products;
    private List<Product> productsForManufacturer;
    private String selectedManufacturer;
    private String selectedProduct;

    static {
        manufacturerNames = new String[3];
        manufacturerNames[0] = "Intel";
        manufacturerNames[1] = "Asus";
        manufacturerNames[2] = "AMD";

        productNames = new String[5];
        productNames[0] = "Motherboard";
        productNames[1] = "Processor";
        productNames[2] = "Graphics card";
        productNames[3] = "NIC";
        productNames[4] = "RAM";
    }

    @PostConstruct
    public void init() {
        manufacturers = new ArrayList<Manufacturer>();

        for(String manufactureName : manufacturerNames) {
            manufacturers.add(new Manufacturer(manufactureName));
        }

        products = new ArrayList<Product>();

        for (String productName : productNames) {
            products.add(new Product(productName, getRandomManufacturerName()));
        }

        productsForManufacturer = new ArrayList<Product>();
    }

    public void updateProduct(AjaxBehaviorEvent event) {
        if(selectedManufacturer == null || selectedManufacturer.trim().length() == 0) {
            return;
        }       

        productsForManufacturer.clear();

        for(Product product : products) {
            if(product.getManufactureName().equals(selectedManufacturer)) {
                productsForManufacturer.add(product);
            }
        }           
    }

    private String getRandomManufacturerName() {
        return manufacturerNames[(int) (Math.random() * 3)];
    }

    public List<Manufacturer> getManufacturers() {
        return manufacturers;
    }

    public void setManufacturers(List<Manufacturer> manufacturers) {
        this.manufacturers = manufacturers;
    }

    public List<Product> getProductsForManufacturer() {
        return productsForManufacturer;
    }

    public void setProductsForManufacturer(List<Product> productsForManufacturer) {
        this.productsForManufacturer = productsForManufacturer;
    }

    public String getSelectedManufacturer() {
        return selectedManufacturer;
    }

    public void setSelectedManufacturer(String selectedManufacturer) {
        this.selectedManufacturer = selectedManufacturer;
    }

    public String getSelectedProduct() {
        return selectedProduct;
    }

    public void setSelectedProduct(String selectedProduct) {
        this.selectedProduct = selectedProduct;
    }       
}

And the xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:p="http://primefaces.org/ui" template="/WEB-INF/templates/globalTemplate.xhtml">

    <ui:define name="title">15369641</ui:define>
    <ui:define name="content">
        <p:growl id="growl" showDetail="true" />

        <h:form id="form">
            <p:selectOneMenu id="manufacturer" value="#{so15369641.selectedManufacturer}">
                <f:selectItem itemLabel="Select One" itemValue="" />
                <f:selectItems value="#{so15369641.manufacturers}" var="manufacturer" itemLabel="#{manufacturer.name}" itemValue="#{manufacturer.name}" />

                <p:ajax listener="#{so15369641.updateProduct}" update="product"/>
            </p:selectOneMenu>

            <p:selectOneMenu id="product" value="#{so15369641.selectedProduct}">
                <f:selectItem itemLabel="Select One" itemValue=""/>
                <f:selectItems value="#{so15369641.productsForManufacturer}" var="product" itemLabel="#{product.name}" itemValue="#{product.name}"/>
            </p:selectOneMenu>
        </h:form>
    </ui:define>

</ui:composition>

Hope it will help.

Environment:

  • JBoss AS7
  • JSF Mojarra 2.1.7 by JBoss org
  • Primefaces 3.4.2

Upvotes: 0

Tapas Bose
Tapas Bose

Reputation: 29806

Change the menu as:

<p:selectOneMenu id="manufectureName" value="#{deviceRegistration.selectedManufacture}" effect="fade" converter="deviceManufactureConverter"
    style="width:200px">
    <f:selectItem itemLabel="Select One" itemValue="" />
    <f:selectItems value="#{deviceRegistration.manufactureNameList}" var="manufacturer" itemLabel="#{manufacturer.manufacturersName}"
        itemValue="#{manufacturer}" />
    <p:ajax update="deviceosName" listener="#{deviceRegistration.updateOSName}" />
</p:selectOneMenu>

The default event of p:ajax is change. Is your listener method get called? Also you don't need :main:deviceosName, since deviceosName is within the same from with manufectureName.

Upvotes: 0

Related Questions