I want my p:selectOneRadio to have value of index of selected radio button.
For example, rows in p:dataTable can be aware of their index using attribute rowIndexVar="rowIndex".
So my question is, can individual radio buttons in p:selectOneRadio be aware of their index? Then i could do something like this
<p:selectOneRadio itemIndexVar="index" value="#{bean.selectedIndex}">
<f:selectItems value="#{bean.items}" var="item" itemValue="#{index}" itemLabel="#{item.label}" />
</p:selectOneRadio>
EDIT:
If i specify items manually, i can indeed set itemValue to represent index of that item, see below.
<p:selectOneRadio id="options" value="#{formBean.selectedIndex}">
<f:selectItem itemLabel="First item has index 0" itemValue="0" />
<f:selectItem itemLabel="Second item has index 1" itemValue="1" />
<f:selectItem itemLabel="Third item has index 2" itemValue="2" />
</p:selectOneRadio>
This would make my p:selectOneRadio to have value corresponding to index of selected radiobutton.
I want to do the same thing, but using f:selectItems tag instead of multiple f:selectItem
Related
I use selectize search on jsf, firstly i chose a value from the dropdown and then when I clear it from the writing area, the selected value remains the same. But i want to set the selected value null after clear value on the text search field. This is the code down below:
Note: I tried itemValue="#{null}" and hideNoSelectionOption on selectOneMenu
<h:selectOneMenu id="nameID" styleClass="select form-control chosen-select search selectize-search" value="#{bean.name}" required="false" requiredMessage="Please choose one">
<f:selectItem itemValue="" itemLabel="None" itemDisabled="false" noSelectionOption="true"/>
<f:selectItems value="#{bean.nameList}" />
</h:selectOneMenu>
I have a p:selectOneMenu and in that list of values where the first value is "Select One" and other values are actual values fetching from db.
When the user selects other values then a dialog box is displaying and fetching respective values, however, after that if user select "Select One" then also displaying the dialog box. Here, I don't want to display dialog box when user select "Select One" from the dropdown.
Xhtml Code:
<p:selectOneMenu id="drp_modify"
value="#{BackingBean.Name}" panelStyle="width:180px"
effect="fade" style="width:180px" filter="true"
filterMatchMode="startsWith" onchange="PF('dlg_modify').show();"
title="Add">
<p:ajax listener="#{BackingBean.onNameChange}"
update="Name_ID, address" />
<f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{BackingBean.nameItemList}" />
</p:selectOneMenu>
<p:dialog header="Modify" widgetVar="dlg_modify" resizable="false">
//// some code
</p:dialog>
You can use JavaScript to check the current value of the combobox.
Instead of onchange="PF('dlg_modify').show();" you can write:
onchange="if(document.getElementById('yourFormId:drp_modify_input').value != '') PF('dlg_modify').show();"
where yourFormId is the ID of your form and the condition is the value given as an itemValue parameter in f:selectItem. In your case it's ''.
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
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>
I am new to JSF and i am stuck at one place. I have one dropdown list which contains two items.
Bank
Cash
Now, if I select bank the other dropdown should populate items related to bank and if I select cash, cash items should be shown in 2nd dropdown.
How to do it in JSF? Where I have to make changes?
Any help will be highly appreciated.Thanks
Well you can use primefaces to improve jsf development
<p:selectOneMenu id="cboCountries" value="#{beanCountries.ValueSelected}" effect="fade">
<f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
<p:ajax event="change" update="cboCities" listener="beanCountries.listCities()"/> //notice "update" refresh an especific DOM Element of an especific ID
</p:selectOneMenu>
<p:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}" effect="fade">
<f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array
</p:selectOneMenu>
" is a special mark from primefaces, the event = "change" will invoke the you select an element from the first "ComboBox" then the property update will refresh the second comboBox, and the property "listener" will do the logical action of what you want to do, in this case the method "listCities()" will fill "listOfCities" with values from DB o Arrays.
So in jsf without primefaces will be like this:
<h:form>
<h:selectOneMenu value="#{beanCountries.ValueSelected}">
<f:selectItem itemLabel="Select Country" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCountries.listOfCountries}" var="countries" itemLabel="#{countries.name}" itemValue="#{countries.id}"/>
</h:selectOneMenu>
<h:selectOneMenu id="cboCities" value="#{beanCities.ValueSelected}">
<f:selectItem itemLabel="Select City" itemValue="-1" /> //a default value
<f:selectItems value="#{beanCities.listOfCities}" var="cities" itemLabel="#{cities.name}" itemValue="#{cities.id}"/> //this is when you want to put elements from DB or from an Array
</h:selectOneMenu>
<h:commandButton value="Submit" action="beanCountries.listCities()"/>
</h:form>
this way is with a button (in jsf) and the action property will execute a java Method and then refresh the page. also this could be made by "ValueChangeListener" property of the selectOneMenu component