PrimeFaces DataTable CellEdit get entity/object

ChrisGeo picture ChrisGeo · Sep 4, 2013 · Viewed 10.8k times · Source

I have a datatable which displays various entities based on a List<>. When I select a cell for editing I want to be able to also get the entity somehow in order to update it. Of course there is event.getRowIndex, which I can then use with the List<>, but that is not always convenient. Is there perhaps another way to get the entity from CellEditEvent?

Answer

BalusC picture BalusC · Sep 4, 2013

One way would be to programmatically EL-evaluate the current <p:dataTable var>.

Given a

<p:dataTable value="#{bean.entities}" var="entity">

you could get it as follows

public void onCellEdit(CellEditEvent event) {
    FacesContext context = FacesContext.getCurrentInstance();
    Entity entity = context.getApplication().evaluateExpressionGet(context, "#{entity}", Entity.class);
    // ...
}

Another way, if you're not interested in the CellEditEvent argument, would be to override the CellEditEvent argument altogether by passing the currently iterated entity as argument instead:

<p:ajax event="cellEdit" listener="#{bean.onCellEdit(entity)}" />

with

public void onCellEdit(Entity entity) {
    // ...
}

Please note that you cannot keep the CellEditEvent and pass additional arguments. This answer would otherwise obviously have been given.