0

I have a Webapp with the Technologies: Wildfly, JSF, Primefaces

I have to select One Menus. The first Select One Menu loads all Warehouses and the second Select One Menu loads all Products which depends to the selected Warehouse.

I want to implemente the posibility, that i don't have necessarily choose a warehouse. I want to have the possibility that i dont select a warehouse, and logically if i dont choose a warehouse, the product Select one menu has to be disabled.

If i submit the form without a warehouse, it should be written in the database null for warehouse and also null for product.

My first attempt was to do the following entry in the select one menu of warehouse.

<f:selectItem itemLabel="---" itemValue="#{null}" value="#{null}"/>

Now, i don't know how to set the value of the warehouse select one menu to null, if i haven't choose a warehouse.

value="#{warehouseDataActions.actualWarehouse}" --> how to set to null 

The second Problem is, that in the Ajax of the first select one menu i have to add the Attribute immediate = true. This Attribute is only neccassary for me if i add the following line. I don't know why ?

<f:selectItem itemLabel="---" itemValue="#{null}" value="#{null}"/>

So i have three questions:

1-How to set the actualWarehouse and .actualWriteservice.warehouseProduct to null ?

  1. How to disable the product select one menu if i have already doesn not choose a warehouse (---) above ?

  2. Why does my Ajax request in the warehouse select one menu does not function, if i add the item

    <p:selectOneMenu style="width:151px"
    value="#{warehouseDataActions.actualWarehouse}">
        <f:converter converterId="ccWarehouseConverter" />
        <f:selectItem itemLabel="---" itemValue="#{null}" value="#{null}"/>
        <f:selectItems
            value="#{warehouseDataActions.allWarehousesForProject}"
            var="warehouse"
            itemLabel="#{warehouse.warehouseName}"
            itemValue="#{warehouse}" />
        <p:ajax listener="#{warehouseProductDataActions.warehouseProductsForWarehouse}"
            update="products" />
    

    <p:panelGrid>
        <p:outputLabel for="products">#{texts['writeservice.product']}</p:outputLabel>
        <p:selectOneMenu id="products" style="width:151px"
            value="#{writeserviceDataActions.actualWriteservice.warehouseProduct}">
            <f:converter converterId="ccWarehouseProductConverter" />
            <f:selectItems
                value="#{warehouseProductDataActions.warehouseProductsResult}"
                var="warehouseProduct" itemLabel="#{warehouseProduct.product.productName}"
                itemValue="#{warehouseProduct}" />
        </p:selectOneMenu>
    </p:panelGrid>
    
java java
  • 405
  • 1
  • 11
  • 25
  • 1) That's default behavior already. 2) Just use `disabled` attribute? 3) Post a MCVE. A broken converter can cause trouble with 1) and 3). The question as stated in the title doesn't cover your actual problems at all. It's already answered in a.o. http://stackoverflow.com/questions/11505248/how-to-load-and-display-dependent-hselectonemenu-on-change-of-a-hselectonemenu/ – BalusC Nov 11 '15 at 11:54

1 Answers1

-1
  1. You need to handle the --- case in your converter (ccWarehouseConverter).

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0 || value.equalsIgnoreCase("---")) {
            return null;
        }
        return databaseService.getWarehouseByName(value);
    }
    
  2. To disable the selectOne

    <p:panelGrid>
      <p:outputLabel for="products">#{texts['writeservice.product']}</p:outputLabel>
      <p:selectOneMenu id="products" style="width:151px" disabled="#{empty warehouseDataActions.actualWarehouse}"
        value="#{writeserviceDataActions.actualWriteservice.warehouseProduct}">
        <f:converter converterId="ccWarehouseProductConverter" />
        <f:selectItems
            value="#{warehouseProductDataActions.warehouseProductsResult}"
            var="warehouseProduct" itemLabel="#{warehouseProduct.product.productName}"
            itemValue="#{warehouseProduct}" />
       </p:selectOneMenu>
    </p:panelGrid>
    

In order to have your ajax update working, you must encapsulate both warehouse and products in a mother panel, and you update the mother:

<h:panelGroup id="motherPanel" layout="block">
    <p:selectOneMenu style="width:151px" value="#{warehouseDataActions.actualWarehouse}">
        <f:converter converterId="ccWarehouseConverter" />
        <f:selectItem itemLabel="---" itemValue="#{null}" value="#{null}"/>
        <f:selectItems
        value="#{warehouseDataActions.allWarehousesForProject}"
        var="warehouse"
        itemLabel="#{warehouse.warehouseName}"
        itemValue="#{warehouse}" />
          <p:ajax listener="#{warehouseProductDataActions.warehouseProductsForWarehouse}"
        update="motherPanel" />
      </p:selectOneMenu>
      <p:panelGrid>
          <p:outputLabel for="products">#{texts['writeservice.product']}</p:outputLabel>
          <p:selectOneMenu id="products" style="width:151px" disabled="#{empty warehouseDataActions.actualWarehouse}"
            value="#{writeserviceDataActions.actualWriteservice.warehouseProduct}">
            <f:converter converterId="ccWarehouseProductConverter" />
            <f:selectItems
                value="#{warehouseProductDataActions.warehouseProductsResult}"
                var="warehouseProduct" itemLabel="#{warehouseProduct.product.productName}"
                itemValue="#{warehouseProduct}" />
           </p:selectOneMenu>
        </p:panelGrid>
</h:panelGroup>
Yamada
  • 723
  • 6
  • 23