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();
Related
I am working on a business intelligence dashboard. On the filter pane, they all have a pencil icon that you can click to edit that specific filter. The issue is that all 12 filters have the same element. How do I select the individual filter pencil?
<div class="ew-i-fx ew-i-act f-act" data-ng-click="levelMainAction($event, level, $index)" data-ng-show="!item.disabled && !item.locked" data-ng-class="{running: opened.edit == 'l'+$index}" data-translate="" data-translate-attr-title="we.actions.editfilter" title="Edit Filter"></div>
If all the attributes of all the filters are same and if the position of the filters are not changing, then you can use index to identity the filter.
You can use the below xpath to find the element and change the index value accordingly:
WebElement element = driver.findElement(By.xpath("(//div[#title='Edit Filter'])[1]"));
For each of these icon I am sure there must be a text or label or title associated to it on the left.
For example -
Filter Name 1 ---- icon
Filter Name 2 ---- icon
and so on...
What you will have to do is first locate "Filter Name 1" element and then locate next icon associated to it.
Will be helpful if you could add more details into your post to show the Filter names and their HTML code.
Basically the HTML above / before
<div class="ew-i-fx ew-i-act f-act" data-ng-click="levelMainAction($event, level, $index)" data-ng-show="!item.disabled && !item.locked" data-ng-class="{running: opened.edit == 'l'+$index}" data-translate="" data-translate-attr-title="we.actions.editfilter" title="Edit Filter"></div>
If you sure with the elements having same properties : By.xpath("(//div[#title='Edit Filter']) and it more than one, then collect them by :
import java.util.List;
import org.openqa.selenium.WebElement;
List<WebElement> elements = driver.findElements(By.xpath("(//div[#title='Edit Filter']));
And the use indexValue to get the element you want :
int indexValue = 1;
elements.get(indexValue).click();
I wants to verify value that is present in Display Name field.
After entering First Name Display Name will populates automatically.
display name value is present in front-end but not present in DOM.
I tried with
driver.webdriver.findElement(By.id("DisplayName")).getText();
and
String abc= driver.webdriver.findElement(By.id("DisplayName")).getAttribute("value");
driver.findElement(By.xpath(".//*[#id='DisplayName']")).getAttribute("value");
Hi this works for me...
I have the following dropdown box created for the user to select a specific year:
<springform:select id="firstYear" path="member.attributes[firstYear].value" value=" " tabindex="3">
<option disabled="disabled" value=" " selected="selected">Select year:</option>
<springform:options id="years" items="${years}"/>
</springform:select>
However, I want to set so that if a user checks a particular checkbox, this dropdown is cleared and a blank value is saved to the DB. Currently it is saving the first value in the array that is ${years} which is not desirable. Here is the javascript I have tried using to clear it:
$("#firstYear option:selected").prop("selected", false);
$("#firstYear").selectedIndex=-1;
I have also tried setting the value of firstYear to " " but this didn't work either.
Any help would be very much appreciated!
You can set the value to the dropdown, so when a time to save it to database it will take that value.
$("#firstYear").val("");
It will set an empty value to the dropdown. Also note, that empty values are treated some databases as NULL. Blank values contains at least one blank symbol and it's not empty string.
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)
I have two dropdowns in html. Both dropdowns are getting data mysql first dropdown is "hostgroup" having value like "windows,linux" and second dropdown is "host" having value like"office,home,sidearea,localhost"
Basically I want to do it so when I select "linux" is hostgroup combobox it will filter "host" dropdown box and show only localhost in "host" dropdown and when I select "windows" in hostgroup dropdown it will filter it and remove localhost from host dropdown
My code for filling dropdown in html is,
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshostgroup.next())
{
html.append("<option value='"+rshostgroup.getString(2)+"'>"+rshostgroup.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
html.append("</td>");
html.append("</tr>");
html.append("<tr>");
html.append("<td>");
html.append("Host");
html.append("</td>");
html.append("<td>");
html.append("<select id='hosts' name='hosts' style='width: 180px' onchange=\"document.forms['form1'].submit();\">>");
//if(rs != null)
//{
while(rshost.next())
{
html.append("<option value='"+rshost.getString(2)+"'>"+rshost.getString(1)+"</option>");
//html.append("<option value='web'>web</option>");
}
//}
html.append("<select>");
Please dont get confuse that what html.append is and what is option value comming from
Actually I am using string builder appending my string builder html in .java(class) file and calling class html appended method in jsp. It is working fine and the option value of drop down is filling from ResultSet rs and rshost having data from mysql.
Firstly using java to build HTML element is a terribly bad idea.
Use JSTL to loop through the dropdown options instead of using Java to spit out HTML code.
Secondly the problem you mention has nothing to do with Java and everything to do with JavaScript.
Well, not entirely true as you might have to make an AJAX request to server get values to populate your secondary dropdown box.
What you need to do is attach a onChange listener for the primary dropdown so that when it changes your javascript code either makes a AJAX call to server to get values for your secondary dropdown box, or use hardcoded values held in some javascript variables.
You will have to loop through the secondary dropdown to remove all previous values and add new dropdown options to it or alternately you can remove the secondary dropdown box entirely and recreate it with the new values based on the selection of the primary dropdown box.
There's already more than enough articles in the internet so use your fiend Google to find the ones that suits your needs closely.