Rerender multiple h:panelGrid on change of h:selectOneMenu in JSF 1.2 - 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>

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

Default value from a list not passed to the next page while using <p:selectOneMenu> in jsf datatable

Default value from a list not passed to the next page while using in jsf datatable,all the default values get replaced with null value for all the items not selected in the datatable
You need to save the list using the managedbean in session scope.
Heading ##
<!-- <h:outputText value="#{rule.userAlertCategory.label}"/> -->
<p:selectOneMenu id="catCombo" value="#{rule.userAlertCategory}" style = "width:100%" converter="srUserAlertCategorySelectItemsConverter">
<p:ajax event="change" update="subCatCombo" listener="#{newAlertSetupController.onCategoryChanged}" process="#this"/>
<f:selectItem itemLabel="Select One" itemValue="" noSelectionOption="true" />
<f:selectItems value="#{newAlertSetupController.userAlertCategoryList}" var="cat" itemLabel="#{cat.label}" itemValue="#{cat}"/>
</p:selectOneMenu>
</p:column>

changing values of selectOneMenu in JSF

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

Richfaces and JSF/AJAX Lifecycles

I´ve managed to get the code to do what I intended, but I do not understand a particular aspect of why this works. I´m running Seam 2.2.2 with Richfaces 3.3.3.
Here is the code from the xhtml page:
...
<h:form id="radiobuttontestform">
<fieldset>
<h:panelGrid columns="3">
<h:selectOneRadio layout="pageDirection" id="myRadio" value="#{actionBean.myRadioButton}">
<f:selectItem itemLabel="First" itemValue="0" />
<f:selectItem itemLabel="Second" itemValue="1" />
<a4j:support event="onclick" ajaxSingle="true" process="myDropdown" reRender="myDropdown,myCount,test" />
</h:selectOneRadio>
<h:panelGrid columns="1" border="0">
<h:selectOneListbox size="1" id="myDropdown" value="#{actionBean.rowCountPredefined}" disabled="#{actionBean.myRadioButton != '0'}">
<f:selectItem itemLabel="10" itemValue="10" />
<f:selectItem itemLabel="20" itemValue="20" />
<f:selectItem itemLabel="30" itemValue="30" />
</h:selectOneListbox>
<h:inputText id="myCount" maxlength="5" value="#{actionBean.rowCountSpecified}" disabled="#{actionBean.myRadioButton != '1'}" required="true">
<f:validateLongRange minimum="1" maximum="1000" />
<rich:ajaxValidator event="onkeyup" for="myCount" />
</h:inputText>
<rich:message id="errorMessage" for="myCount" ajaxRendered="true" showDetail="false" showSummary="true">
<f:facet name="errorMarker">ERROR:</f:facet>
</rich:message>
</h:panelGrid>
</h:panelGrid>
</fieldset>
<h:outputLabel id="test" value="RadioValue: #{actionBean.myRadioButton}" />
<a4j:commandButton id="show" value="Show Values in Log" action="#{actionBean.showValues}" />
<a4j:commandButton id="done" value="Save and end conversation" action="#{actionBean.apply}" />
</h:form>
...
The backing bean is just a simple POJO with getters and setters for the three properties here. (myRadioButton, rowCountPredefined, rowCountSpecified)
This is what I get: (the correct result)
radio button example http://katzekat.de/Bilder/radio2.png
radio button example http://katzekat.de/Bilder/radio.png
Here is my thinking:
Setting ajaxSingle to true means that only the radio button will be processed on the server. The dropdown next to it doesn´t need validation - it will always contain a correct value. I´ve added the process="myDropdown" in order to persist the value into the backing bean, otherwise when I switch the radio button to position 2 the dropdown reverts to its original value. (I realise this is only cosmetic!) I´ve checked this with a debugger and it does indeed set the property in the backing bean and everything is working as expected.
Once the radio button is switched into position 2 a value can be entered in the textbox and will be validated. This works perfectly, and when I switch the radio button back to position 1, the validation error is cleared if this field is in an error state. Presumably because the error message is only present in the request scope.
Firing up the debugger again when the radio button is in position 2 and entering a valid value in the text field reveals no update on the backing bean when switching back to position 1. I also expected this as I´m telling only the radio and the dropdown to process on the server. This is the bit I don´t understand though - the value in the textfield is persisted on postback. (See 2nd link above) Where is this value for this textfield saved if not in the backing bean ?
Submitted form values are stored in component on Apply Request Values phase.
For example in your case javax.faces.component.html.HtmlInputText#decode method of the textfield calls javax.faces.component.UIInput#setSubmittedValue. The submitted values are not set to the bean (as you expected) as the component is not included to execute part. Then inputText's renderer re-displays (writes to the response) the submitted values.
It works pretty the same when validation fails. Submitted values are stored on Apply Request Values phase, and then because of failed validation the submitted values are not set to beans (Update Model Values phase is skipped), then the submitted values are re-displayed.

Conditionally render a component based on selectOneMenu value

Is there a way to render a component based on the current value the user has selected from a selectOneMenu component? My selectOneMenu component is populated with an enum consisting of two values, smoker and non-smoker. If the user has smoker selected I want to render a checkbox below it which lets the user check how many they smoke a day 10, 20, 30+, etc. I also want the opposite to work if they user has selected non-smoker i.e. the check boxes don't render/disappear.
Just check the dropdown menu's value in the rendered attribute of the target components and update their common parent by a <f:ajax>. Here's a kickoff example:
<h:selectOneMenu value="#{bean.item}">
<f:selectItem itemValue="one" />
<f:selectItem itemValue="two" />
<f:selectItem itemValue="three" />
<f:ajax render="results" />
</h:selectOneMenu>
<h:panelGroup id="results">
<h:panelGroup rendered="#{bean.item eq 'one'}">
You have selected "one".
</h:panelGroup>
<h:panelGroup rendered="#{bean.item eq 'two'}">
You have selected "two".
</h:panelGroup>
<h:panelGroup rendered="#{bean.item eq 'three'}">
You have selected "three".
</h:panelGroup>
</h:panelGroup>
If you'd like to perform some business logic based on the selected value, use <f:ajax listener>.
<f:ajax listener="#{bean.changeItem}" render="results" />
public void changeItem() {
someResult = someService.getByItem(item);
}
See also:
Why do I need to nest a component with rendered="#{some}" in another component when I want to ajax-update it?
How to load and display dependent h:selectOneMenu on change of a h:selectOneMenu
Conditionally displaying JSF components

Resources