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.
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.
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:
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>
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.
When running the application, you will encounter an ADFv error.
The developers guide (22.5.1) has the solution. DO NOT check ‘enable selection’ when you want to use multiple selection in a table.
If you remove the properties selectionListener and selectedRowKeys from your table, it will work as expected.
A sample workspace can be downloaded here.
I have an ADF Tree binding which i access in the managed bean.In my code I have done  bnd_detailTree.getSelectedRowKeys().removeAll() and when i check for bnd_detailTree.getSelectedRowKeys().size(), it returns 0, but when i check i find   bnd_detailTree.getSelectedRowKeys()!=null and the (JUCtrlHierNodeBinding)bnd_detailTree.getRowData() returns the row which was selected. I am not able to understand the behaviour, could you please explain?
Emiel, You can use the accessKey attribute of the commandbutton. If you set this for instance, accessKey=”A” , the button will be rendered with text “select All”; (A being underlined). Depending on your browser, you can invoke the button with the mnemonic key.
Indeed much easier as I expected. Do you also know if it’s possible to change or add a mnemonic key? For example add Alt-a as key combi and underline the letter a of “Select All”?