If you are a regular user of ADF and the checkbox component you might have already saw the Steve Muench's demo app of creating a checkbox in table in 10.1.3 (117. Editable Checkbox in an ADF Faces Table [10.1.3.2] 19-JUN-07). Here you'll see creating the same kind of demo application with checkbox as one of the table column.One more added feature you will see here is the Select all /Deselect all checkbox on the column header.
Components : JDev 11g production verion available at otn for free download.
Oracle XE Free database with a schema with EMP table.
Add the Sports_Intrested (Varchar2(1)) column to the EMP table which we will be using to show the check box in the ui.
Sports intrested column is a varchar2(1) column which stores the values 'Y' or 'N' depending
upon a Employee is intrested in sports or not.
Create a EmpEO ,EmpVO and a CheckBoxDemoAM using the EMP table.
leave the attributes to their default types.SportsIntrested Attribute will be a String Attribute.
Create a Test page and drop the Emp VO instance under the CheckBoxDemoAM on the test page to create a ADF faces table.
This would render the column SportsIntrested as a simple Output text column showing the Y or N values.
And the TestPG will only have singe tree binding to the Emp1Iterator.
Now delete the sportsIntrested Output text from the SportsIntrested Column.(Leave the column intact , only delete the output text.We need the Column to create the checkbox.)
Drop the SportsIntrested attributes on the TestPG in the SportsIntrested Column as a selectOneBooleanCheckbox.
The wizard will ask you for the values which should be mapped to the checkbox when it is checked or unchecked.give the value 'Y' for the selected value and the value 'N' for the unselected.Now do two things.
1.set the autosubmit value on the checkbox to true, unless you do this your checkbox value is not submitted.
2.give a value to the id property of the checkbox and set it as one of the partial trigger to the af table.
The newly created checkbox will create a button binding to map the checkbox check/uncheck condition depending upon the attribute value.
Create the Commit and Rollback buttons which you can always use to test you application from the database.
That's It your 11g checkbox demo is ready!!..
Now we shall add the more intresting feature to the table i.e the select all/deselect all checkbox.
Add a Checkbox to the header facet of the checkbox column.Give it an id say selectAllCheckBox.give it a label say "Select All".
Create a value change listener method to this checkbox by creating a new Managed bean.
Modify the following code and place inside the newly created bean.
public void selectAllCheckBoxVCL(ValueChangeEvent valueChangeEvent) {
System.out.println("xdebug c1 : In selectAllChoiceBoxLN with value = "+
valueChangeEvent.getNewValue());
boolean isSelected = ((Boolean)valueChangeEvent.getNewValue()).booleanValue();
DCBindingContainer dcb = (DCBindingContainer) evaluateEL("#{bindings}");
DCIteratorBinding dciter =dcb.findIteratorBinding("EmpVO1Iterator");
ViewObject vo = dciter.getViewObject();
int i = 0;
Row row = null;
vo.reset();
while (vo.hasNext()) {
if (i == 0)
row = vo.first();
else
row = vo.next();
// System.out.println("Changing row 1: " +
// row.getAttribute("Name"));
System.out.println("xdebug c2: Changing row 2: " +
row.getAttribute("SportsIntrested"));
if(isSelected)
row.setAttribute("SportsIntrested", "Y");
else
row.setAttribute("SportsIntrested", "N");
i++;
}
}
private static Object evaluateEL(String el) {
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
ExpressionFactory expressionFactory =
facesContext.getApplication().getExpressionFactory();
ValueExpression exp =
expressionFactory.createValueExpression(elContext, el,
Object.class);
return exp.getValue(elContext);
}
One more importing thing is again to set this checkbox autosubmit to true and the af table partial trigger to this checkbox.Checking the checkbox on the column header level would select all the columns in the table.
Run the TestPG and you'll see the desired results.
Unchecking on the checkbox on the column header level would un check all the columns in the table.