ADF 11g: Select all rows in an ADF table.

I get a lot of questions on how to select all rows in a table. In this post I describe how you can do that. A common use case for this is when you want to apply changes on a set of records, for instance change the status of all records in a table. You might expect a whole bunch of code here (so did I…) but in fact, this is very easy. I’ll provide the (absolutely not complicated) solution and also the proof that it works

In this example I created ADF BC for only the EMPLOYEES table in the HR schema, created a taskflow with pagefragments, a pagefragment to hold the employeesTable and a main page to render the taskflow in a region. I ‘ADF bind’ the table and I set the table’s rowSelection property to “multiple”, so it supports multiple row selection.

Here’s the trick

There is no trick ! You are all set to select all rows in the table, by a single key stroke ! “Ctrl+a”
Put you cursor anywhere in the table, press “Ctrl+a” and be amazed.
ADF 11g: Select all rows in an ADF table. allRowsSelected

I hear you say: Ok, that is cool, all rows selected client side, but what about the real question: Are those rows really selected ?
Here’s your proof. Create a button to invoke a method in a backing bean, to show the number of selected rows, and all the selected rowkeys.
ADF 11g: Select all rows in an ADF table. check
The code in the bean will just count the number of selected rows, and print out it’s rowkeys.

public void checkSelected(ActionEvent actionEvent) {
// Add event code here...

Iterator it = empTable.getSelectedRowKeys().iterator();

System.out.println("we have " + empTable.getSelectedRowKeys().size() + " rows selected");
while (it.hasNext()) {
Object key = it.next();
System.out.println(key);
}
}

If you press “Crtl+a” and after that the “checkSelected” button, you will see the proof in the log:
ADF 11g: Select all rows in an ADF table. proof

What if I want a button to select all rows ?

It is not uncommon to have users that are not aware of the all shortcut keystrokes in an application. For them it would come in handy to have a button or menu item to select all rows. First you create the button.

<af:commandToolbarButton partialSubmit="true" text="selectAll" id="cb1"
actionListener="#{multiSelectBean.selectAll}">
</af:commandToolbarButton>

ADF 11g: Select all rows in an ADF table. selectAllButton
You wire up the action Listener to the button and write a bunch of code to select all rows in the table.

public void selectAll(ActionEvent actionEvent) {
// Add event code here...
selectAllRowsInTable();
}

public void selectAllRowsInTable() {
RowKeySet rks = new RowKeySetImpl();

CollectionModel model = (CollectionModel)empTable.getValue();

int rowcount = model.getRowCount();

for (int i = 0; i < rowcount; i++) {
model.setRowIndex(i);
Object key = model.getRowKey();
rks.add(key);
}

empTable.setSelectedRowKeys(rks);
}

Invoking the ‘selectAll’ button will result in all rows being selected. Try for yourselves, and invoke the ‘checkSelected’ button to verify the selection.

Common mistake.
When dropping the employees collection as a table on your pagefragment, you might make a mistake. Because we need to be able to select records in the table, it makes sense to check the ‘enable selection’ checkbox.
ADF 11g: Select all rows in an ADF table. rowselectionOn
When running the application, you will encounter an ADFv error.
ADF 11g: Select all rows in an ADF table.

The developers guide (22.5.1) has the solution. DO NOT check ‘enable selection’ when you want to use multiple selection in a table.
ADF 11g: Select all rows in an ADF table. devguideNote

If you remove the properties selectionListener and selectedRowKeys from your table, it will work as expected.

A sample workspace can be downloaded here.

3 Comments

  1. Divya February 1, 2012
  2. Luc Bors July 30, 2010
  3. Emiel Paasschens July 30, 2010