Menu

JSF: update single entry in a datalist like component

Goal

To update a single entry in a datalist like component in a JSF application

Description

Recently, in a project I was working on, there was the need to create a GUI that would make it possible to show a list of elements that, each, could be made editable and, when editable, the interface of that element would change so that it could present the editable components.

This recipe explains how to achieve this kind of behaviour.

How to

Use a view build time tag such as JSTL’s forEach instead of a view render time tag such as Primefaces’ dataList or dataTable, as follows:

And make sure you specify a panel id which uses the varStatus variable to force a different name for each iteration. From now on, we may refer to the wrapping panel group whose name was specified as “refundRequest#{refundRequestStatus.index}”, in the included file refund_request_view.xhtml, as :form:refundRequest#{refundRequestStatus.index}. A simplification of that file with the most relevant contents is shown next:

Explanations

Now, we can update each “refundRequest” independently, because we really are able to refer to each of them. As explained by BalusC comment here, with a component such as dataList or dataTable, the server side does not generate an id per iteration. That part only occurs for the client side, which means there is no way to refer to it in an Ajax call.

Additionally, we not have to name each of the components using the varStatus variable to make sure the name of the component is unique within the view root tree. So, we now have to define each component id as follows: