Wednesday, January 18, 2012

ADF common errors: How to get selected row from table ?

In many blogs I found above question and solution like this:

FacesCtrlHierBinding rowBinding = (FacesCtrlHierBinding) tab.getSelectedRow();
Row rw = rowBinding.getRow();

or 

RichTable _table = (RichTable ) selectionEvent.getSource();
CollectionModel model = (CollectionModel ) _table.getValue();
FacesCtrlHierBinding _binding = (FacesCtrlHierBinding) model.getWrappedData();
FacesCtrlHierBinding.FacesModel treeModel = (FacesCtrlHierBinding.FacesModel) _binding.getTreeModel();
treeModel.makeCurrent(selectionEvent);


On first sight FacesCtrlHierBinding exposes the convenient methods you need. But the solution is invalid. You shouldn't use FacesCtrlHierBinding and FacesCtrlHierNodeBinding directly cause they are internal ADF Faces component model for tree, table and treeTable.

You can use EL in Java: #{bindings.myEntityView.treeModel.makeCurrent}:
  • Easy to use
  • Does it right for all Data Control types
  • Requires knowledge about the PageDef definition
or



Use JUCtrlHierBinding or JUCtrlHierNodeBinding
  • No knowledge about binding layer is required
  • Needs more code to write
By example:


RichTable _table = (RichTable ) selectionEvent.getSource();
CollectionModel model = (CollectionModel ) _table.getValue();
JUCtrlHierBinding _binding = (JUCtrlHierBinding) model.getWrappedData();
DCIteratorBinding iteratorBinding = _binding.getDCIteratorBinding();
Object _selectedRowData = _table´.getSelectedRowData();
JUCtrlHierNodeBinding node = (JUCtrlHierNodeBinding) _selectedRowData ;
Key rwKey = node.getRowKey();
iteratorBinding.setCurrentRowWithKey(rwKey.toStringFormat(true));

2 comments:

  1. Hi I have a tree table. I want to get the multiple selected values.I have created the tree table programmatically. Can you tell me how can I get the selected values.

    ReplyDelete
  2. ADFUtils.invokeEL("#{bindings.Employee1.collectionModel.makeCurrent}", new Class[]{SelectionEvent.class}, new Object[]{selectionEvent});

    ReplyDelete