Hugo
Hugo

Reputation: 45

primefaces cascade selectonemenu

I have 3 dropdowns, the second has to be loaded based on the selected option of the first. And the third based on the selected option of the second. This is the page:

<h:panelGrid columns="2" cellpadding="5">
    <p:outputLabel value="Empresa:" for="selectCompany" />
    <p:selectOneMenu id="selectCompany" value="#{dropdownBean.company}"
        converter="entityConverter" effect="fade" effectSpeed="200"
        var="company">

        <p:ajax process="@this" event="change"
            update="selectBranch, selectVendor" />

        <f:selectItems value="#{dropdownBean.companies}" var="companyItem"
            itemLabel="#{companyItem.corporateName}" itemValue="#{companyItem}" />
        <p:column>
            <h:outputText value="#{company.corporateName}" />
        </p:column>
    </p:selectOneMenu>
</h:panelGrid>

<h:panelGrid columns="2" cellpadding="5">
    <p:outputLabel value="Surcursal:" for="#selectBranch" />
    <p:selectOneMenu id="#selectBranch" value="#{dropdownBean.branch}"
        converter="entityConverter" effect="fade" effectSpeed="200"
        var="branch">

    <p:ajax process="@this" event="change" update="#selectVendor" />

        <f:selectItem itemLabel="SELECCIONE SUCURSAL" itemValue=""
            noSelectionOption="true" />
        <f:selectItems value="#{dropdownBean.branches}" var="branchItem"
            itemLabel="#{branchItem.branchName}" itemValue="#{branchItem}" />
        <p:column>
            <h:outputText value="#{branch.branchName}" />
        </p:column>
    </p:selectOneMenu>
</h:panelGrid>
<h:panelGrid columns="2" cellpadding="5">
    <p:outputLabel value="Vendedor: " for="#selectVendor" />
    <p:selectOneMenu id="#selectVendor" value="#{dropdownBean.vendor}"
        converter="entityConverter" effect="fade" effectSpeed="200"
        var="vendor">
        <f:selectItem itemLabel="SELECCIONE VENDEDOR" itemValue=""
            noSelectionOption="true" />
        <f:selectItems value="#{dropdownBean.vendors}" var="vendorItem"
            itemLabel="#{vendorItem.vendorName}" itemValue="#{vendorItem}" />
        <p:column>
            <h:outputText value="#{vendor.vendorName}" />
        </p:column>
    </p:selectOneMenu>
</h:panelGrid>

And the backbean:

@ManagedBean
public class DropdownBean {
@Inject
private CompanyBC companyBC;
@Inject
private BranchBC branchBC;
@Inject
private VendorBC vendorBC;

private V_Companies company;
private V_Branches branch;
private V_Vendors vendor;

private List<V_Companies> companies;
private List<V_Branches> branches;
private List<V_Vendors> vendors;

@PostConstruct
public void allCompanies() {
    companies = companyBC.allCompanies();
}

public List<V_Companies> getCompanies() {
    return companies;
}

public void setCompanies(List<V_Companies> companies) {
    this.companies = companies;
}

public List<V_Branches> getBranches() {
    if(company!=null)
        branches = branchBC.allCompanyBranches(company.getCompanyId());
    return branches;
}

public void setBranches(List<V_Branches> branches) {
    this.branches = branches;
}

public List<V_Vendors> getVendors() {
    if(company!=null && branch!=null)
        vendors = vendorBC.allBranchVendors(company.getCompanyId(), branch.getBranchId());
    return vendors;
}

public void setVendors(List<V_Vendors> vendors) {
    this.vendors = vendors;
}

public V_Companies getCompany() {
    return company;
}

public void setCompany(V_Companies company) {
    this.company = company;
}

public V_Branches getBranch() {
    return branch;
}

public void setBranch(V_Branches branch) {
    this.branch = branch;
}

public V_Vendors getVendor() {
    return vendor;
}

public void setVendor(V_Vendors vendor) {
    this.vendor = vendor;
}

}

The first two dropdowns work great but the last one does not. Apparently cannot be more than one p:ajax for the same event or something like that. Any suggestions please?

Upvotes: 1

Views: 1826

Answers (1)

Hugo
Hugo

Reputation: 45

adding a listener worked:

 <p:ajax process="@this" listener="#{userEditMB.onBranchChange}" event="change" update="selectVendor" />


public void onBranchChange(AjaxBehaviorEvent event) {

    setBranch((V_Branches) ((UIOutput) event.getSource()).getValue());

    vendors = vendorBC.allBranchVendors(getBranch().getCompanyId(),
            getBranch().getBranchId());
}

I tried listener before but passing the Id's as parameters. Passing AjaxBehaviorEvent and casting to the object was the answer for me. I hope helps someone Thanks!

Upvotes: 1

Related Questions