Is it possible to change EL base on the selected value from dropdown list? - jsf

<th>
<h:outputText value="Pending Actions" style="font-weight:bold" />
<td colspan="4">
<p:selectOneMenu value="#{selectedValue}" required="true">
<f:selectItem itemValue="#{null}" itemLabel="--Select One--" />
<f:selectItem itemValue="organization1" itemLabel="ORGANIZAION1" />
<f:selectItem itemValue="organization2" itemLabel="ORGANIZAION2" />
<f:selectItem itemValue="organization3" itemLabel="ORGANIZAION3" />
<f:selectItem itemValue="organization4" itemLabel="ORGANIZAION4" />
</p:selectOneMenu>
<h:inputTextarea value="#{casesBean.organization1PendingActionDetails}"/>
</td>
</th>
As shown above, there is a dropdown list to let users select which organization they want to assign task.
In my database, there are columns
organization1PendingActionDetails,
organization2PendingActionDetails, etc.
So my question is:
Is it possible to change the inputTextarea value
based on the selected value from the dropdown list?
For example:
If the user selected organization2 then the inputTextarea will become value="#{casesBean.organization2PendingActionDetails}"

You will probably need to have more than one inputTextarea. Each area should have different value, and rendered set based on selected value. After selection set proper rendered value on inputTextarea and update it via ajax.
<p:ajax event="change" listener=#{casesBean.decideRenderedInputTextArea} update="InputTextAreasWrapper" />
Better solution:
It is way more elegant to have one inputTextArea. As you said you want to store the values from it into the database. So decide where to store data based on selected value inside been before performing action which will store data into database (after submit form).

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

Get string behind binding object

I have the following JSF construct:
<c:set var="myVar" value="#{myBean.getMyMap()}" scope="request" />
<h:form>
<p>
<h:outputText value="Output1: " />
<h:selectOneMenu value="#{myMappingsBean.data.attribute1}" binding="#{input1}" required="true">
<f:selectItems var="entry" value="#{myVar}"/>
<p:ajax update="myDropdown"/>
</h:selectOneMenu>
</p>
<p>
<h:outputText value="Output2: " />
<h:selectOneMenu id="myDropdown" value="#{myMappingsBean.data.attribute2}" binding="#{input2}" required="true" converter="javax.faces.Double">
<f:selectItems var="entry" value="#{myVar.get(input1.value)}"/>
</h:selectOneMenu>
</p>
</h:form>
Behind myVar is a map defined like that: Map<String, Collection<Double>>
The first dropdown menu shows a list of all the keys of that map (like desired), but the value behind that is the collection of values.
Here the HTML output of one option of that dropdown:
<option value="[1.0, 2.0]">SomeString</option>
My second dropdown should list the collection of double values stored in the map behind the key, that is selected by the first menu.
The problem now is, when I use value="#{myVar.get(input1.value)}" the value I get from .value is the collection and not the string/key of the map. So I never get the desired result.
How can I get the string/key behind the binded object input1? Is there something like input1.name or .toString? Is somewhere a documentary for this?
Ok, I solved it by applying the solution from here.
The first drop down has to be edited to the following:
<p>
<h:outputText value="Output1: " />
<h:selectOneMenu value="#{myMappingsBean.data.attribute1}" binding="#{input1}" required="true">
<f:selectItems var="entry" value="#{myVar.entrySet()}" itemValue="#{entry.key}" itemLabel="#{entry.key}"/>
</h:selectOneMenu>
</p>
As you can see, the entry set is created from the map and from that you use the key as value and label.
And with that the value behind input1.value now is not the collection of doubles but the key of the map.

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>

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

SelectOneMenu with Delete link

I am wondering is there a way i can add delete() in the select one menu, so that the user either can select one from the menu or delete the record right from there itself. So far i have normal select one menu, i have a method called "#{statusReport.deleteTieckt}" method. I just wondering how to add that to the below select one menu.
<ice:selectOneMenu value="#{statusReport.projectDetID}"
style="width: 350px;" partialSubmit="true"
onchange="this.form.submit()" immediate="true"
valueChangeListener="#{statusReport.retrieveReport}">
<f:ajax disabled="true"></f:ajax>
<f:selectItem itemLabel="Select One" itemValue="" />
<f:selectItems value="#{statusReport.listOfProjectDetail}"
var="projectBO"
itemLabel="#{projectBO.projectDtObj.pname} #{projectBO.startDate} - #{projectBO.endDate}"
itemValue="#{projectBO.pdetailId}" noSelectionValue="true" />
</ice:selectOneMenu>

Resources