For multiple row selection (assuming a RichTable reference called myDataTable, based on a collection of MyObject objects):
// get the selected rows from a table component public void getMultipleRows(ActionEvent actionEvent) { RichTable table = getMyDataTable(); for (Object facesRowKey : table.getSelectedRowKeys()) { table.setRowKey(facesRowKey); Object o = table.getRowData(); MyObject selectedItem = (MyObject)o; if (selectedItem != null) { // process the item } } }For single selection:
public Object getSelectedRow(RichTable table) { Object _selectedRowData = table.getSelectedRowData(); JUCtrlHierNodeBinding _nodeBinding = (JUCtrlHierNodeBinding)_selectedRowData; return _nodeBinding.getRow(); }
So we can define a Selection Listener for the table, and this way we can get the source table component from the event itself:
// to get the selected Rows public void itemSelected(SelectionEvent selectionEvent) { RichTable table = (RichTable)selectionEvent.getSource(); DCDataRow dcrow = (DCDataRow)getSelectedRow(table); MyObject selectedItem = (MyObject)dcrow.getDataProvider(); // TODO ... }
If the table is backed by a View Object, we can cast the selected row to a ViewRowImpl object:
public void itemSelected(SelectionEvent selectionEvent) { RichTable table = (RichTable)selectionEvent.getSource(); ViewRowImpl row = (ViewRowImpl)getSelectedRow(table); if (row != null) { String attr1 = (String)row.getAttribute("attr1"); String attr2 = (String)row.getAttribute("attr2"); // ... } }
Hi Matteo,
ReplyDeleteThanks for the post.
I am facing an issue with a table backed by view object and i see the last snippet in your post be the possible solution but when i try to add this line
ViewRowImpl row = (ViewRowImpl)getSelectedRow(table);
it says method not found which i dont see in the documentation too. Any help on this please
Thanks.
Hi ,
ReplyDeleteViewRowImpl selected;
for this generate accessors then write code like this
Row row=this.getselectedrow();