Sunday, November 1, 2009

Filling SelectOneChoice From Backingbean

On of the major component in tha ADF RC component is a af:selectOneChoice. You can fill this component from backingbean by tow ways:

A) I not prefer this method because we use many objects and for loop to fill SelectOneChoice Component but I will illustrate it:
1-In the backingbean you can define a variable that hold the selected Value (the value which you are select from selectOneChoice component ) [e.g public String selectedValue;] and make its setter and getter.
2- In the backingbean you can define an array of selectItem that will fill the selectOneChoice(e.g. public SelectItem[] elements = null; ) and make its getter and setter.
3- In the getter of selectItem write this code :
BindingContainer bindings = getBindings();
DCIteratorBinding dciter =(DCIteratorBinding) bindings.get("UCustomerView1Iterator");
int length = dciter.getViewObject().getRowCount();
elements = new SelectItem[length];
SelectItem item = null;
for (int i = 0; i < length; i++) {
Row rw = dciter.getRowAtRangeIndex(i);
if (rw != null) {
item = new SelectItem();
item.setValue(rw.getAttribute("Id").toString());
item.setLabel(rw.getAttribute("Name").toString());
}
elements[i] = item;
}
return elements;
where : UCustomerView1Iterator is the Iterator which its data will fill the selectOneChoice (You must add this iterator in the pageDef).

You backingbean it should be like this :
-----------------------------------------------
public SelectItem[] elements = null;
public String selectedValue;

private BindingContainer bindings;

public BindingContainer getBindings() {
if (this.bindings == null) {
FacesContext fc = FacesContext.getCurrentInstance();
this.bindings =
(BindingContainer)fc.getApplication().evaluateExpressionGet(fc,
"#{bindings}",
BindingContainer.class);
}
return this.bindings;
}
public SelectItem[] getElements() {
BindingContainer bindings = getBindings();
DCIteratorBinding dciter =(DCIteratorBinding) bindings.get("UCustomerView1Iterator");
int length =
dciter.getRangeSize() > 0 ? dciter.getRangeSize() : new Long(dciter.getEstimatedRowCount()).intValue();
elements = new SelectItem[length];
SelectItem item = null;
for (int i = 0; i < length; i++) {
Row rw = dciter.getRowAtRangeIndex(i);
if (rw != null) {
item = new SelectItem();
item.setValue(rw.getAttribute("Id").toString());
item.setLabel(rw.getAttribute("Name").toString());
}
elements[i] = item;
}
return elements;
}

public void setElements(SelectItem[] elements) {
this.elements = elements;
}
public void setSelectedValue(String selectedValue) {
this.selectedValue = selectedValue;
}
public String getSelectedValue() {
return selectedValue;
}
4- In you page Drage a selectOneChoice component and set its value to :
#{yourBean.selectedValue}.
5- Insert f:selectItems inside af:selectOneChoice and set its value to:
#{yourBean.elements}.
--------------------------------------------------------------------------------
B) The second method to fill the selectOneChioce is to use af:forEach component and I prefer this method :
1- In the pageDef make a table in the binding section that will fill your selectOneChoice(assume that you make a DeptView1 table in the binding.
2- DeptView1 Table is point to the DeptView1Iterator make its length to -1.
3- Drag and drop a selectOneChoice component and set its value to the attribute in the backingbean.
4- Insert inside selectOneChoice af:forEach component and set Items property to : #{bindings.DeptView1.rangeSet} and set Var property to row.
5- Insert Inside af:forEach af:selectItem set Value Property to #{row.Deptno} this represent the return value of the selectOneChoice if you want the return value be a name of the department set Value Property to #{row.Dname}. Set the Label Property to #{row.Dname}.(Where Deptno and Dname is the attribute of the iterator you use to fill selectOneChoice).
you can download an example from here.

1 comment: