本文介绍了PrimeFaces DataTable CellEdit获取实体/对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表,该数据表根据列表显示各种实体.当我选择一个单元进行编辑时,我希望能够以某种方式获取该实体以便对其进行更新.当然有event.getRowIndex,我可以将其与List<>一起使用,但这并不总是很方便.也许还有另一种方法可以从CellEditEvent中获取实体?

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?

推荐答案

一种方法是通过编程方式对当前<p:dataTable var>进行EL评估.

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

给出一个

<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);
    // ...
}

如果您对CellEditEvent参数不感兴趣,另一种方法是通过传递当前迭代的实体作为参数来完全覆盖CellEditEvent参数:

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)}" />

使用

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

请注意,您不能保留CellEditEvent并传递其他参数.否则显然会给出这个答案.

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

这篇关于PrimeFaces DataTable CellEdit获取实体/对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 08:40