Friday, February 6, 2009

ADF 11g : CheckBox Demo (Select one checkbox in table, Select all/Deselect all)

Note : This post is incomplete and still under progress ....

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.

Run the TestPG and you'll see the desired results.
Checking the checkbox on the column header level would select all the columns in the table.















Unchecking on the checkbox on the column header level would un check all the columns in the table.

11 comments:

Unknown said...

Hello,
Whenever i convert a text item to a boolean check box and do select the check box, i still get null value when setting the check box.
Any ideas?

Thanks.

Nisha said...

Hi,

Thanks for this useful blog. I have implemented a boolean checkbox column in a table usinga transient attribute in the VO and it is working fine.I am trying to do a select all feature and I am having issues with all the checkbox showing selected.The value gets set to true for every row in the backing bean method but just that it is not showing in the table display when i select the select all checkbox. It would be great if you could help.

VMS said...

the ExpressionFactoryImpl what import is?
com.sun.el.ExpressionFactoryImpl ???

Bala Krishna N. Chitneni said...

@Nisha: Try using String "Y"/"N" instead of Boolean true/false in transient attribute.

Unknown said...

Hi

I have a requirement where we have to display around 10 checkboxes on a page. Based on the checkboxes selected, we need to display the text of those checkboxes in the format of a table. So, can you please help.

Thanks
Srikala

Unknown said...

Hi,

Thanks for this useful explanation. But if uncheck the table rows, the select All check box still checked. Please help me to resolve this issue

Pruthiviraj said...

when I used to selectAll boolean checkbox for the first time then all the row checboxes are not getting selected. But the moment I deselect and then selectAll then it's working. One more thing is that if I click on selectAll checkbox then the rows which are visible on the table are getting selected but the moment I scroll down in the table then the subsequent rows are remaining as not selected.

Shahzaib said...

Its not working ...

mahisanthiblog said...

Excellent.. Its working fine for me. Thanks

Unknown said...

hi

when i select all rows aree selected,but when i deselect rows are not deselected.

chocovito_007 said...

I just need to select a row in a table in adf 11g, the table has it's view with the key, I just don't know how can we just select one row but, when you open other tab and back to that tab, the table goes to the first line, and I want to go to a specific line, and apear selected that row...