Getting selected row of ScrollableDataTable
peterjca Jul 6, 2010 6:55 AMHi,
I've just joined this community as I've started using RichFaces 3.3.3 and JSF 1.2. Around 4 years ago I gained massive experience with MyFaces which I used to generate dynamic GUIs programatically at runtime to edit content whose structure a user could define at runtime. All good fun but annoying too.
Again with RichFaces I'm creating a custom component programatically. The component provides a form in which to enter property values for a database query and displays the results, if any, in a scrollable data table. You can then select a row in the table for further processing. It is this last part that has stumped me. I'm obviously interested in getting the selected row (or, actually an ID/primary key of the object) back to the server.
Some snippets of pertinent code follow.
Here's my table creation:
final HtmlScrollableDataTable dataTable = new HtmlScrollableDataTable();
final String dataTableId = parent.getId() + "_resultstable";
dataTable.setId(dataTableId);
dataTable.setSelectionMode(SelectionMode.single);
dataTable.setVar("myItem");
Here's the AJAX support creation:
final HtmlAjaxSupport ajaxRowClick = new HtmlAjaxSupport();
ajaxRowClick.setId(dataTable.getId() + "rowClick");
ajaxRowClick.setEvent("onselectionchange");
dataTable.getFacets().put("a4jsupport", ajaxRowClick);
Here's a first attempt at having a selection change callback to a backing bean and/or backing bean setter calls with the id of the object in the row (the id being a Long). This snippet of code successfully calls the selectionChanged(ActionEvent event) method but from the event I can't seem to get the "selectedRow" parameter. Also, the backing bean methods are not called at all (I know this because I've got some println statements in them).
final MethodExpression actionExpr = expressionFactory
.createMethodExpression(elContext,
"#{policySearchBean.selectionChanged}", void.class,
new Class[] { ActionEvent.class });
HtmlActionParameter actionParam = new HtmlActionParameter();
actionParam.setId(ajaxRowClick.getId() + "_param1");
actionParam.setName("selectedRow");
final ValueExpression myExpr1 = expressionFactory
.createValueExpression(elContext, "#{myItem.policyId}",
Long.class);
actionParam.setValueExpression("value", myExpr1);
final ValueExpression myExpr2 = expressionFactory
.createValueExpression(elContext,
"#{policySearchBean.policyId}", Long.class);
actionParam.setAssignToBinding(myExpr2);
actionParam.setActionListener(actionExpr);
ajaxRowClick.addActionListener(actionParam);
My second try is the following:
SetPropertyActionListener listener = new SetPropertyActionListener();
final ValueExpression myExpr1 = expressionFactory.createValueExpression(elContext, "#{myItem.policyId}", Long.class);
listener.setValue(myExpr1);
final ValueExpression myExpr2 = expressionFactory.createValueExpression(elContext, "#{policySearchBean.policyId}", Long.class);
listener.setTarget(myExpr2);
ajaxRowClick.addActionListener(listener);
This does call the setters in the backing bean but the IDs are always 0. I guess this is a JSF lifecycle issue.
Note that I'm not using any of the "row var" stuff on the table. My custom tag is within an a4j:form tag.
Sadly, as with MyFaces all those years ago, RichFaces doesn't really have any good documentation beyond reference material. Fundamentals such as getting the selected row index/object/ID shouldn't be this hard?!