Reputation: 27899
How to know if contents of any column/s of the row being edited in a <p:dataTable>
is changed? Given a simple example as follows.
The data table :
<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>
The managed 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.");
}
}
Is there a way to invoke business service (to update database contents) if and only if the row currently being edited in the given <p:dataTable>
is actually changed?
There could be more columns in <p:dataTable>
as and when required as obvious.
Upvotes: 2
Views: 1885
Reputation: 16273
Add this property to the bean:
private Brand original;
In the dataTable, add a listener for a rowEditInit event:
<p:ajax event="rowEditInit" listener="#{testManagedBean.onRowEditInit()}" />
Set the property value when the rowEditInit event is triggered:
public void onRowEditInit(RowEditEvent event) {
original = (Brand) event.getObject();
}
Finally, do the checks in onRowEdit()
:
public void onRowEdit(RowEditEvent event) {
Brand modified = (Brand) event.getObject();
// compare modified against original
}
Upvotes: 1