如何知道<p:dataTable>
中正在编辑的行的任何列的内容是否已更改?给出一个简单的例子如下。
数据表:
<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>
中可能会有更多列,并且在需要时显而易见。
答案 0 :(得分:0)
将此属性添加到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
}