Conditionally rendering <f:selectItem> inside <c:if>,<ui:fragment> - jsf

I need to display No Jurisdiction to add in the Jurisdiction dropdown when the given condition satisfies.[condition:When there is no Jurisdiction available,it should display the above text in dropdown.If there are jurisdictions available,the above text should not be displayed].And I dont want to make it disabled.
The problem i am facing is whether it does or does not displays No Jurisdiction to add irrespective of the condition provided.
<p:selectOneMenu id="JurisdictionName"
value="#{ApplicationManagedBean62.saveRequestMap['Jurisdiction']}"
<f:selectItem itemLabel="Select" itemValue="" />
<c:if test="#{empty ApplicationManagedBean62.knowledgeValueMap['Object::AddJurisdictionStatelist']}">
<f:selectItem itemLabel="No Jurisdiction to add" itemValue="" />
</c:if>
<f:selectItems
value="#{ApplicationManagedBean62.knowledgeValueMap['Object::AddJurisdictionStatelist']}"
var="Jurisdiction"
itemLabel="#{Jurisdiction['StateRegionCode']}"
itemValue="#{Jurisdiction['StateRegionCode']}" />
</p:selectOneMenu>
I also tried,
<ui:fragment rendered="#{empty ApplicationManagedBean62.knowledgeValueMap['Object::AddJurisdictionStatelist']}">
<f:selectItem itemLabel="No Jurisdiction to add" itemValue="No Jurisdiction to add" />
</ui:fragment>
Kindly help me out.

You can't do it like that you have 2 choices.
Use itemDisabled="true" or noSelectionOption="true" on items you don't want selectable and they will be greyed out. See: https://javaee.github.io/glassfish/doc/5.0/vdldoc/f/selectItem.html
Generate your selectItems server side and filter out the ones you know should not be there like this...
<f:selectItems value="#{myBean.jurisdictions}" var="type" itemLabel="#{type.displayText}" itemValue="#{type}" />

Related

How enable/disable <p:tab> without update form?

I have a big <h:form>, with a <p:tabView> with 3 <p:tab>
I want to enable/disable one <p:tab> based on <p:selectOneRadio> click.
How can I achieve this without updating the entire form?
<p:selectOneRadio id = "pessoa" required = "true"
value="#{myBean.typeChecked}" valueChangeListener="#{myBean.checkTypeSelec}">
<f:selectItem itemLabel="item 1" itemValue="PF" />
<f:selectItem itemLabel="item 2" itemValue="PJ" />
<p:ajax event="click" update="???" execute="#this"/>
</p:selectOneRadio>
<p:tab title="History" id="historyTab" disabled="#{myBean.historyDisabled}">...
I dont wanna update entire form because I dont wanna loose other inputs data

selectOneMenu calls value setter twice

I have a selectOneMenu and the issue with it is that it updates the value twice!! first time with the value I chose, immediately after that it updates the value with the number it previously had!
<p:selectOneMenu id="qupdate" value="#{object.pquantity}">
<f:selectItem styleClass="form-control"
itemLabel="-- SELECT QUANTITY -- " itemValue=""
noSelectionOption="true" />
<f:selectItems value="#{selectonemenu.quantoptions}" var="f"
itemLabel="#{f}" itemValue="#{f}" />
<p:ajax execute="qupdate" event="change"
listener="#{Bean.quantitychange(object.pquantity, object.id)}" />
</p:selectOneMenu>
is there anyway to have this working, I have tried to trace and it actually calls the setter twice!
#Holger is correct in the comments. p:selectOneMenu does not require a listener to actually process the value change. One should use a listener only to actually make other kind of interactions in the bean.
In this case, the fixed code should look like this:
<p:selectOneMenu id="qupdate" value="#{object.pquantity}">
<f:selectItem styleClass="form-control"
itemLabel="-- SELECT QUANTITY -- "
itemValue=""
noSelectionOption="true" />
<f:selectItems value="#{selectonemenu.quantoptions}"
var="f"
itemLabel="#{f}"
itemValue="#{f}" />
</p:selectOneMenu>
For more information about how the ajax listener is supposed to be used, please refer to to this question.

JSF Bootsfaces b:selectOneMenu itemValue = null displaying the label value instead

I am using Bootsfaces 1.0.2. The code below, of course, is inside a form. Notice the first selectItem has an itemValue=#{null}. When the page loads, the inputText field with id='test' shows as blank. I then change the dropdown to any other value and the value displays correctly. When I go back to the first option in the drop down with null value, the inputText field displays "-- Seleccione --". What I would expect to see is blank (NULL), right? Or am I missing something here?
<b:selectOneMenu value="#{wCPVMController.wcpvm.tipo_v}"
id="categoriaVehiculo" label="CategorĂ­a de VehĂ­culo"
required="true" process="#this" update="containerRiesgos test"
onchange="ajax:wCPVMController.getRiesgosOpcionales()">
<f:selectItem itemLabel="-- Seleccione --" itemValue="#{null}" />
<f:selectItem itemLabel="Liviano" itemValue="L" />
<f:selectItem itemLabel="Pesado" itemValue="P" />
<f:selectItem itemLabel="Autobus" itemValue="A" />
<f:selectItem itemLabel="Motocicleta" itemValue="M" />
<f:selectItem itemLabel="Otro" itemValue="O" />
</b:selectOneMenu>
<b:inputText id="test" value="#{wCPVMController.wcpvm.tipo_v}"></b:inputText>
Write instead itemValue=""
See example here:
https://showcase.bootsfaces.net/forms/selectOneMenu.jsf

Rerender multiple h:panelGrid on change of h:selectOneMenu in JSF 1.2

The situation:
I want to display specific panelGrid(s) on the page based on drop-down selection. I am doing this by storing several boolean which get set to true/false based on which item is selected in the drop down. I use the onchange="submit()" to refresh/re-render the page.
The problem:
I have validation on many fields in the form, so if I select an item it does validation and will not display the form.
The question:
How do I get the selectOneMenu change to re-render only the set of controls on the page being impacted instead of the whole page and causing validation?
The code: (JSF 1.2)
<h:selectOneMenu id="list" value="#{sessionBean.myDecorator.selectedItem}"
onchange="submit()" immediate="true" required="true"
valueChangeListener="#{requestBean.listChanged}">
<f:selectItem itemValue="#{null}" itemLabel="--Select One--" noSelectionOption="true" />
<f:selectItems value="#{sessionBean.myDecorator.mapItems}" />
</h:selectOneMenu>
Field causing validation:
<h:inputTextarea id="lblRequiredField" cols="100" rows="3" required="true" immediate="true"
value="#{sessionBean.myDecorator.myDo.myField}">
<f:validateLength minimum="1" maximum="300" />
</h:inputTextarea>

set default value for selectonemenu

I have a select menu like this :
<h:selectOneMenu value="#{serverMB.selectedServerType}" >
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}" render="selectservertype"/>
</h:selectOneMenu>
I want the default value of the select menu to be the value of one object that has been filled before, something like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}" default="serverMB.server.servertype"> // <- default
or like this:
<h:selectOneMenu value="#{serverMB.selectedServerType}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem value"serverMB.server.servertype"/> <- but this item was the first one and has been selected
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}" />
</h:selectOneMenu>
You can do something like this if your object is already populated when you call the page, if it is not, it will show the <f:selectItem/> message:
<h:selectOneMenu value="#{serverMB.server.servertype}">
<f:converter binding="#{serverTypeConverter}"/>
<f:selectItem itemLabel="Select a Server..." />
<f:selectItems value="#{serverMB.serverTypesList}" var="servertypes"
itemLabel="#{servertypes.server_type_name}"/>
<f:ajax listener="#{serverMB.changeSelectedOneMenuServerType}"
render="selectservertype"/>
</h:selectOneMenu>
You can also use the OmniFaces converter.
Here is my example. It works fine for me. (Note: Year is a object. Make sure, you have equals implemented. Year is name and id only.)
<h:selectOneMenu value="#{bean.user.year}"
converter="omnifaces.SelectItemsConverter">
<f:selectItem itemLabel="Please select a year." itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems value="#{bean.allyears}" var="year"
itemValue="#{year}" itemLabel="#{year.name}" />
</h:selectOneMenu>

Resources