Problem with deleting table rows using ctrl+a for row selection
- by Frank Nimphius
Normal
0
false
false
false
EN-US
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;}
The following code is commonly shown and
documented for how to access the row key of selected table rows in an ADF Faces
table configured for multi row selection.
public void onRemoveSelectedTableRows(ActionEvent actionEvent) {
RichTable richTable = … get access to your table instance …
CollectionModel cm =(CollectionModel)richTable.getValue();
RowKeySet rowKeySet = (RowKeySet)richTable.getSelectedRowKeys();
for (Object key : rowKeySet) {
richTable.setRowKey(key);
JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)cm.getRowData();
// do something with rowData e.g.update, print, copy
}
//optional, if you changed data, refresh the table
AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(richTable);
return null;
}
The code shown above works for 99.5 % of all
use cases that deal with multi row selection enabled ADF Faces tables, except
for when users use the ctrl+a key to mark all rows for delete. Just to make
sure I am clear: if you use ctrl+a to mark rows to perform any other operation
on them – like bulk updating all rows for a specific attribute – then this
works with the code shown above. Even for bulk row delete, any other mean of
row selection (shift+click and multiple ctrl+click) works like a charm and the
rows are deleted.
So apparently it is the use of ctrl+a that
causes the problem when deleting multiple rows of an ADF Faces table. To
implement code that works for all table selection use cases, including the one
to delete all table rows in one go, you use the code shown below.
public void onRemoveSelectedTableRows(ActionEvent actionEvent) {
RichTable richTable = … get access to your table instance …
CollectionModel cm = (CollectionModel)richTable.getValue();
RowKeySet rowKeySet = (RowKeySet)richTable.getSelectedRowKeys();
Object[] rowKeySetArray = rowKeySet.toArray();
for (Object key : rowKeySetArray){
richTable.setRowKey(key);
JUCtrlHierNodeBinding rowData = (JUCtrlHierNodeBinding)cm.getRowData();
rowData.getRow().remove();
}
AdfFacesContext adfFacesContext = AdfFacesContext.getCurrentInstance();
adfFacesContext.addPartialTarget(richTable);
}