jsf primefaces, show itemLabel not itemvalue on update - java

I have 2 items:
radiobuttons
outputtext
What I'm trying to do is: When the user checks a radiobutton -> get the itemLabel of the checked radiobutton in my outputtext. (not itemValue)
I have following code which shows the itemValue in the output text:
<h:form>
<p:selectOneRadio layout="pageDirection" id="test" name="testy" value="#{myBean.testValue}">
<p:ajax update="testOutput"/>
<p:selectItems value="#{myBean.getAllTestItems()}" var="selecter" itemLabel="#{selecter.label}" itemValue="#{selecter.val}"/>
</p:selectOneRadio>
<h:outputText id="testOutput" value="#{myBean.testValue}"/>
</h:form>
When i select one of the radiobuttons, I get the itemValue in the outputText. However I would like to get the itemLabel of the selected item in the outputText.
How do I get the label instead of the value of the radiobutton?

Several ways.
Get it based on the collection behind #{myBean.getAllTestItems()} which already contains the items with both the label and the value. Find the item matching the selected value in there and then get the label.
Submit #{selecter} instead of #{selecter.val} as radio button item value. This way you can display the label by #{myBean.testValue.label}. You only need to provide a Converter to convert between String (in HTML and HTTP request parameter) and Selecter (in Java model).
See also:
Our selectOneMenu wiki page
How to create Primefaces radioButtons from List? (for the case you'd like to use Selecter as value)

Related

Use currently checked value as a itemValue in ListBox

I have this JSF page:
<h:selectOneRadio id="regions">
<f:selectItem id="eu" value="EU"/>
<f:selectItem id="us" value="US"/>
</h:selectOneRadio>
<h:selectOneListbox>
<f:selectItems value="#{dataFetcherBean.regions}" itemLabel="..."/>
</h:selectOneListbox>
dataFetcherBean.regions is a HashMap<String, List<String>> collection. What I want, is to show different data in ListBox depending on what radio button is selected.
Is there a way to get the currently selected value in regions radio buttons to itemLabel in the ListBox?
You have 2 solutions :
1 - Use a listener to update your <h:selectOneListbox> after the event onchange of your <h:selectOneRadio>is triggered.
2 - Use <f:ajax> if you don't want to reload the full page.
See both here : SelectOneMenu updates other SelectOneMenu

Get the selected value from selectOneChoice in ADF and not the index

I am new to JDeveloper and ADF and I am facing a bit of a problem in getting the selected value from selectOneChoice component. This is the valuChangeListener:
public void versionValueChangeListener(ValueChangeEvent valueChangeEvent) {
System.out.println(valueChangeEvent.getOldValue().toString());
System.out.println(valueChangeEvent.getNewValue().toString());
}
This is giving the index of the selected choice and not the text itself. How can I get the text and not the index?
This is the code for the selectOneChoice:
<af:selectOneChoice value="#{bindings.Version.inputValue}"
label="#{bindings.Version.label}"
required="#{bindings.Version.hints.mandatory}"
shortDesc="#{bindings.Version.hints.tooltip}"
id="soc3" autoSubmit="true"
valueChangeListener="#{savesBean.versionValueChangeListener}">
<f:selectItems value="#{bindings.Version.items}" id="si3"/>
</af:selectOneChoice>
Thanx :)
This is how the guys at Orcle do it
How-to get the selected af:selectOneChoice Label although in my opinion it can be done in other way...
I think you better build a map in which the index will be the key and the value is the label
than in versionValueChangeListener you'll access the map something like this :
myMap.get(valueChangeEvent.getNewValue().toString());
There is a property for the select one choice item on the jsp/jsf page that allows you to pass the actual object value or to pass the index value from the list. Click on the select one choice item in the jsp/jsf page, then hit the bindings tab at the bottom, go to the page definition (you will see it at the top of the bindings page) and the select one choice item will be highlighted in the page definition file that is now open. If you look at the property inspector from here - there is a property called "SelectItemValueMode" by default it is set to the ListIndex value, you can change it to the ListObject from here. It is the last property listed for the select one choice list object in the properties window from the page definition file.
This is not ADF specific. This is HTML specific. Only the value of HTML <input type="radio"> element is been sent, not the value of the HTML <label> element referring it. This is true for every other HTML <input>, <select> and <textarea> element.
The soltion to your "problem" is simple: you already have all labels in your backing bean in the collection behind #{bindings.Version.items}. Just get the label from there based on the selected value.
Alternatively, set the whole complex object (containing both the value and the label) instead of only its value as item value. You only need a Converter to convert between the complex object and string.
You can find out the solution for this on the following URL: https://blogs.oracle.com/adf/entry/getting_selected_value_from_selectonechoice
Assume we have a Model-Driven List for Deptno attribute with the
display value of Dname and selectOneChoice bound to Deptno attribute
in jspx page
<af:selectOneChoice value="#{bindings.Deptno.inputValue}" label="Select Department"
required="true" shortDesc="#{bindings.Deptno.hints.tooltip}"
id="soc1" autoSubmit="true">
<f:selectItems value="#{bindings.Deptno.items}" id="si1"/>
</af:selectOneChoice>
When we want the selected value, the common mistake we do is to use
the same EL bound to the value property of SelectOneChoice component,
but using this we get the index of the selected item instead rather
than the value. This is because when we drag and drop attribute as
SelectOneChoice on to the page, SelectItems gets generated with
indexes as values.
Displaying selected value on to the jspx page
In this section, we see how to get selected value without writing
single line of java code. Create an outputText component with its
value property bound to #{bindings.Deptno.attributeValue} instead of
#{bindings.Deptno.inputValue} and make it refreshable based on list selection by adding partialTriggers property.
<af:outputText value = "Selected Value: #{bindings.Deptno.attributeValue}" id="ot1" partialTriggers="soc1"/>
The above code gives the Deptno value of the selected item. If the
Deptno of 'SALES' is 30, 30 will get displayed on outputText on
selecting 'SALES' from the list.
If we want 'SALES' itself to be displayed then the following EL should
be used assuming Dname is the second attribute DeptView
<af:outputText value = "Display Value: #{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}" id="ot2" partialTriggers="soc1"/>
Inside value change listener
Evaluating above EL expressions inside ValueChangeListener doesn't
give the current selected value instead gives the previously selected
value as the selected value doesn't get updated to the model by the
time ValueChangeListener gets invoked.
In this case, before accessing the selected value, we need to update
the model first.
Here is the sample code:
public void valueChanged(ValueChangeEvent valueChangeEvent) {
this.setValueToEL("#{bindings.Deptno.inputValue}", valueChangeEvent.getNewValue()); //Updates the model
System.out.println("\n******** Selected Value: "+resolveExpression("#{bindings.Deptno.attributeValue}"));
System.out.println("\n******** Display Value: "+resolveExpression("#{bindings.Deptno.selectedValue ne ' ' ? bindings.Deptno.selectedValue.attributeValues[1] : ''}"));
}
public Object resolveExpression(String el) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
ValueExpression valueExp = expressionFactory.createValueExpression(elContext,el,Object.class);
return valueExp.getValue(elContext);
}
public void setValueToEL(String el, Object val) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
ValueExpression exp = expressionFactory.createValueExpression(elContext, el, Object.class);
exp.setValue(elContext, val);
}
This worked for me
BindingContainer binding = BindingContext.getCurrent().getCurrentBindingsEntry();
JUCtrlListBinding fc =(JUCtrlListBinding) binding.get(nameOfTheDataControl);
String selectedValue = (fc.getListIterBinding().getRowAtRangeIndex(newIndexSelected).getAttribute(nameOftheColu
mnInTheDataControl)).toString();

Primefaces datatable: what 'onRowSelectUpdate="display"' means?

I'm using the primefaces datatable and I've seen many times the onRowSelectUpdate event calling "display"?
What "display" means? is it a public method of the component or something?
It refers the ID of the component which is to be ajax-updated whenever you select a row. For example, a detail display.
<p:dataTable id="mytable" onRowSelectUpdate="mydisplay">
...
</p:dataTable>
...
<p:someComponent id="mydisplay">
<p>This component will be ajax-updated whenever a row is selected.
</p:someComponent>
From the PrimeFaces user guide, page 127:
Component(s) to update instantly after a row is selected.
Sounds like it works in the same way as the Ajax update attribute, but is activated when a row is selected in a data table. I've never used it myself so I don't have any code samples to share.

"other" option in dropdown menu and setting values

I have a combobox with an option "other". user can select an entry from the list or select option "other". if they select "other" a text box will disply below to enter a new account number. see the code below:
<td>1. Account Number<span class="bodyCopy"><font color="#ff0000"> * </font></span>:
<html:select name="reDataForm" property="Member.accountNumber" styleClass="formContent"
style="width:80px" onchange="showfield(this.options[this.selectedIndex].value)">
<html:options collection="<%= WorkConstants.NewDropdowns.PACCT %>" property="value" labelProperty="label" styleClass="formContent"/>
</html:select>
<div id="div1"></div>
It works fine. But when the page refreshes, the text box disappears as no code to keep it there.
1)how do I keep the text box if the use was selected "Other" and entered a new account number in the text box ? OR if we can add the text box value into the dropdown list, that will also work.
2) Also how to set the text box value into the same variable Member.accountNumber ?
please help me !!!!
Use a little bit of javascript on an onLoad event to check if other is selected, and display the text box
You can't submit the value as the same name as the select box. instead, submit as a second name and process accordingly in your ActionForm on the serverside.

Struts 2 radio button value.?

i have following code in jsp used with struts2 radio button i want to know which radio button is selected in my action class so can anyone give me solution on this.
code:
<div>
<s:radio cssClass="formFieldRadio" name="selectAction" list="{'Postponed To'}"/>
<s:radio cssClass="formFieldRadio" name="selectAction" list="{'Suspended'}"/>
<s:radio cssClass="formFieldRadio" name="selectAction" list="{'cancelled'}"/>
</div>
i use diffrent radio with same name so that are displayed each in new row...
I dint understand why you need all the radio buttons with the same name.. If the names are different add the getter/setter for all the radio buttons in the actions with there same names, the in action exectute() method check the values of the radio button for true or false as
if(formFieldRadio == true)
u get to know its checked or not
You are doing it wrong, you should do it like this:
<s:radio name="selectAction" list="#{1:'Postponed To', 2: 'Suspended', 3: 'cancelled'}" />
Then in your action you should expect a numeric value (in this case, 1, 2 or 3) in an selectAction variable.
make sure you have the theme="simple" attribute so you can play freely with your css.

Categories

Resources