jsf update drop down menu - jsf

I have two drop down menus, and the content of the second depends on the choice in the first one. Now, the content changes but only after I hit "refresh". It needs to change automatically on selection of the first menu.
<h:outputLabel value="Country: " />
<h:selectOneMenu value="#{bean.country}">
<f:selectItem itemValue="#{null}" itemLabel="-- select country --" />
<f:selectItem itemValue="Spain" itemLabel="Spain" />
(...)
<f:ajax listener="#{bean.findCity}" />
</h:selectOneMenu>
<h:outputLabel value="City: " />
<h:selectOneMenu value="#{bean.city}">
<f:selectItem itemValue="#{null}" itemLabel="-- select city --" />
<f:selectItems value="#{bean.citiesFound}" var="city"
itemLabel="#{city.cityNameFull}" itemValue="#{schedule.cityName}" />
</h:selectOneMenu>
Again, the function findCity works fine, because the result is correct when I refresh the page, but I want the second menu to refresh automatically.

Related

default selected value in h:selectOneMenu

<h:selectOneMenu id="customerSelection" required="true" requiredMessage="Enter/Select a customer"
value="#{engagement.selectedDropdownCustValue}" validator="#{engagement.validateCustomer}">
<f:selectItem itemLabel="Select Customer" itemValue="#{null}" noSelectionOption="true" itemDisabled="true" />
<f:selectItems var="c" id="customer" itemLabel="#{c.name}" itemValue="#{c.name}" value="#{dashboard.customers}" />
<f:selectItem itemLabel="Other" itemValue="#{null}" />
<p:ajax update="engagementCustomer" />
</h:selectOneMenu>
<p:inputText id="engagementCustomer" value="#{engagement.selectedEngagement.customer.name}"
maxlength="30" disabled="#{engagement.selectedDropdownCustValue!=null}" validator="#{engagement.validateCustomer}" />
I want to have 'Select Customer' to be displayed in dropdown after page load, but Other is displayed by default in dropdown. Purpose of this code is to display already existing customers while creating new engagement, but if other value is selected from dropdown then input filed should be enabled and user should be allowed to enter new customer value.

Update Component Even If SelectOneMenu Value Is Required

I have a required select and my problem is that when the user selects an empty option (null) the validation jumps in and the listener call of the select is being skipped. Some components on my view are depending on this value so in case of null being selected the bean won't get actualized and the components don't change.
Example:
<h:form id="createForm">
<p:selectOneMenu id="produktCreateTypSelect"
converter="omnifaces.SelectItemsIndexConverter"
value="#{productController.selectedProduct.typ}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="-- Select --" />
<f:selectItems var="productType" value="#{productController.findAllProducts()}"
itemLabel="#{productType.name}" itemValue="#{productType}" />
<p:ajax process="#this " event="valueChange" update="createForm"
listener="#{productController.printHelloWorld}" />
</p:selectOneMenu>
<h:panelGroup id="dummy1" rendered="#{productController.selectedProduct.type != null}" />
</h:form>
The question is what the best way is to update that panelGroup
'dummy1'
Thanks for any help.

SelectOneMenu depends to other selectOneMenu - Jsf

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 ?
How to disable the product select one menu if i have already doesn not choose a warehouse (---) above ?
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>
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);
}
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>

Reseting p:ajax drop down after un-selecting option

I am using prime faces in my project. I am generating a pull down by using p:ajax and its working fine for me. But i have notice a problem that is if i select ---select one-- my pull down which i have generated by p:ajax is not resetting to default .
My code is:
<p:selectOneMenu id="propExam" value="#{rollNoBean.examination}"
converter="omnifaces.SelectItemsConverter" editable="false"
required="true" label="Examination Applied For" styleClass="dropdownWidth">
<f:selectItem itemLabel="---Select One---" itemValue="0" />
<f:selectItems value="#{rollNoBean.testExamNames}" var="test" itemLabel="#{test.name}" itemValue="#{test}" noSelectionOption="true" />
<p:ajax listener="#{rollNoBean.readFilterByExamination(rollNoBean.examination.id)}"
update="degreeList centerId" />
</p:selectOneMenu>
<h:outputText value="Select Filter Category :" />
<p:selectOneMenu id="degreeList"
value="#{rollNoBean.filter}" editable="false"
converter="omnifaces.SelectItemsConverter" required="true"
label="Please Select degree" styleClass="dropdownWidth">
<f:selectItem itemValue="" itemLabel="---Select One---" />
<f:selectItems value="#{rollNoBean.degreeNames}"
var="degree" itemLabel="#{degree.name}" itemValue="#{degree}" />
</p:selectOneMenu>
and my bean code is:
public List<Filter> readFilterByExamination(int id)
{
if(id!=0)
{
return degreeNames=examinationDetailsService.readFilterByExamination(id);
}
return degreeNames=new ArrayList<Filter>();
}
Please give suggetion

Cannot click on selected Item when the value is null primefaces

I have a selectOneMenu, in my xhtml page, when i want to click in a selectItem with the itemValue is null, there is no effect, it shows the default selection
<p:selectOneMenu id="cout" style="width: 120px;" value="#{serviceManagedBean.selectedService.coutSmsCalc}">
<f:selectItem itemLabel="Sélectionnez une" itemValue="" />
<f:selectItem itemLabel="oui" itemValue="oui" />
<f:selectItem itemLabel="non" itemValue="" />
</p:selectOneMenu>
So, when i click in the itemLabel "non", it remains on "Sélectionnez une"
The selectOneMenu use itemValue to change the displayed value. So if your value is null like the default one, the action changeListener is not called. Try to change the itemValue by empty or other key.
Try this if the item value is a String
<f:selectItem itemLabel="Sélectionnez une" itemValue="{null}" />
<f:selectItem itemLabel="oui" itemValue="oui" />
<f:selectItem itemLabel="non" itemValue="non" />
Or try this if the item value is boolean
<f:selectItem itemLabel="Sélectionnez une" itemValue="{null}" />
<f:selectItem itemLabel="oui" itemValue="true" />
<f:selectItem itemLabel="non" itemValue="false" />

Resources