如何知道在<p:dataTable>
中编辑的行的任何列/s的内容是否发生了变化?给出如下简单的例子。
数据表:
<p:dataTable var="brand" value="#{testManagedBean}"
lazy="true" editable="true" rows="10">
<p:column headerText="Id">
<h:outputText value="#{brand.brandId}"/>
</p:column>
<p:column headerText="Brand">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{brand.brandName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{brand.brandName}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit" width="50">
<p:rowEditor/>
</p:column>
<p:ajax event="rowEdit" listener="#{testManagedBean.onRowEdit}"/>
</p:dataTable>
被管理bean:
@ManagedBean
@ViewScoped
public final class TestManagedBean extends LazyDataModel<Brand> implements Serializable
{
@EJB
private final TestBeanLocal service=null;
private static final long serialVersionUID = 1L;
@Override
public List<Brand> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
setRowCount(3); //Just an example. Actually fetched from the database.
return service.getList(first, pageSize, sortField, sortOrder, filters);
}
public void onRowEdit(RowEditEvent event) {
//Do something to check if the row currently being edited has been changed.
//If something is changed then, invoke an EJB method to propagate changes to the database.
//If nothing is changed then, get rid of invoking an expensive business service method to unnecessarily execute an update query.
System.out.println("onRowEdit() called.");
}
}
是否有一种方法来调用业务服务(更新数据库内容),当且仅当当前在给定<p:dataTable>
中编辑的行实际上被更改?
可以在<p:dataTable>
中添加更多的列。
将此属性添加到bean:
private Brand original;
在dataTable中,为rowEditInit事件添加一个监听器:
<p:ajax event="rowEditInit" listener="#{testManagedBean.onRowEditInit()}" />
设置触发rowEditInit事件时的属性值:
public void onRowEditInit(RowEditEvent event) {
original = (Brand) event.getObject();
}
最后,检查onRowEdit()
:
public void onRowEdit(RowEditEvent event) {
Brand modified = (Brand) event.getObject();
// compare modified against original
}