Showing posts with label selectOneChoice. Show all posts
Showing posts with label selectOneChoice. Show all posts

Saturday, 16 May 2015

ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part2

In my previous post, I have explained how to create the dropdown in the page with only one VO. In the OTN forum, I have noticed that some users have some issues when using that approach. In this post I will explain the Option 1 suggested in my previous post. ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part1

References :
   Ashish's Blog: Implementing custom search form in ADF programmatically
   Sireesha's Blog: Getting selected value from SelectOneChoice
  • Create a Query based View Object DeptVO with DepartmentId and DepartmentName.
  • Create another View Object DummyVO with the below Query. 
 select null DEPARTMENT_ID from dual  
  • Add List of Values to the DepartmentId Attribute of the DummyVO using the DeptVO.
  • Make Sure that you set the Updatable property of the Attribute to 'Always'.
  • Now you can just drop the DummyVO to the page as a ADF Form.
  • To get the values in the ValueChangeListener, you can use the below code (copied from the 2nd Reference mentioned above :)).
   public void valueChangeListener(ValueChangeEvent valueChangeEvent) {  
     System.out.println("Selected Value: "+valueChangeEvent.getNewValue());  
     this.setValueToEL("#{bindings.DepartmentId.inputValue}", valueChangeEvent.getNewValue()); //Updates the model  
     System.out.println("Selected Value: "+resolveExpression("#{bindings.DepartmentId.attributeValue}"));  
     System.out.println("Display Value: "+resolveExpression("#{bindings.DepartmentId.selectedValue ne ' ' ? bindings.DepartmentId.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);  
   }  
  • Now if you want set a default value for the dropdown, you can modify the query used for the DummyVO. For ex:- If you want to display 'Purchasing' as the default value for the dropdown and the corresponding DepartmentId is 30. Modify the query like below:
 select 30 DEPARTMENT_ID from dual  
  • Now when the page loads, the default value selected in the dropdown will be 'Purchasing'.
Sample application built using Jdev 12.1.3 can be found here.

Feel free to point out if anything is missing/wrong in this blog.

Friday, 8 May 2015

ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part1

Recently there was couple of questions in the OTN forum in regards to creating dropdown in the based on values from a table. I googled and couldn't find much results on this. So thought of adding here.

Normally when we need an LOV/dropdown in the page, it will be for an attribute in a VO. So we define the LOV for that attribute in the VO definition.

ex: We will create a DeptVO with DeptId and DeptName and use this VO to create LOV for the departmentId attribute of EmployeeVO.

What if we just need a dropdown in the page with the values coming from a database table.

ex:- Just show a dropdown with all the Department Names.


Option1 : You can create a DummyViewObject with needed attribute(s) and create an LOV on that attribute(s) using the DeptVO. [I prefer this approach. This approach is explained here ADF: How to create SelectOneChoice based on values from a database table in ADF Web Application - Part2]


Option 2:
  • Create a Query based View Object DeptVO with DepartmentId and DepartmentName.
  • You just drag the DeptVO as  a selectOneChoice into the page.
  • Select the DepartmentName as the display attribute.
  • Remove the value property and map it to a Managed Bean variable.
  • Set autoSubmit to true.  (Only if you want to handle the Value Change event)
  • Set the Unselected Label to whatever you want. (ex: ***Select a Value***)
  • You need to select the 'Include No Selection' checkbox in the ViewObject definition.
This is good enough to show the dropdown in the page based on values from the database, without creating additional ViewObject.


Now to get the values in the valueChangeListener event, you can write the below code:
   public void valueChangeEvent(ValueChangeEvent valueChangeEvent) {  
     System.out.println("New Name : " + valueChangeEvent.getNewValue());  
     valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());   
     DCBindingContainer bindingContainer = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();   
     DCIteratorBinding binding = bindingContainer.findIteratorBinding("DeptVO1Iterator");   
     Row newRow = binding.getCurrentRow();   
     System.out.println("New ID: "+newRow.getAttribute("DepartmentId"));   
   }  

If you want to get the selected value on the click of a button, you can just get it from the ManagedBean variable.
   public void buttonActionListener(ActionEvent actionEvent) {  
     System.out.println("New Name : " + this.lovValue);  
   }  

Sample application built using Jdev 12.1.3 can be found here.

Feel free to point out if anything is missing/wrong in this blog.