I'm using Primefaces' p:dataTable to display an editable table and I'm using p:rowEditor to add or delete rows, when adding a row I add an object to the datalist and update the datatable, this works fine, but when I cancel edit I delete the added object to the datatable, the listener is fired but it doesn't update the datatable, any suggestions please. here is the .xhtml:
<h:form id="Users">
<p:dataTable id="datalist"
value="#{beanController.listusr}" var="item"
selectionMode="single" editable="true"
styleClass="datalistdisplayusr"
selection="#{beanController.selectedusr}"
rowKey="#{item.usrId}"
paginator="true" rows="5" rowsPerPageTemplate="5,10,15">
<f:facet name="header">
<p:commandButton id="newusrButton" icon="ui-icon-plus"
value="Create"
actionListener="#{beanController.createusr}"
update=":Users:datalist"
oncomplete="$('.datalistdisplayusr .ui-datatable-data .ui-row-editor .ui-icon-pencil').first().click();"
/>
</f:facet>
<p:ajax event="rowEdit"
listener="#{beanController.saveusr}"
update=":Users:datalist" />
<p:ajax event="rowEditCancel"
listener="#{beanController.onCancel}"
update=":Users:datalist" />
<p:column style="width:6%">
<p:rowEditor />
</p:column>
<p:column >
<f:facet name="header">
<h:outputText value="Id" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.usrId}" />
</f:facet>
<f:facet name="input">
<p:inputText id="Id" value="#{item.usrId}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column >
<f:facet name="header">
<h:outputText value="First Name" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.usrName}" />
</f:facet>
<f:facet name="input">
<p:inputText id="FirstName" value="#{item.usrName}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column >
<f:facet name="header">
<h:outputText value="Last Name" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.usrSurName}" />
</f:facet>
<f:facet name="input">
<p:inputText id="SurName" value="#{item.usrSurName}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
And here is the save method. Till now every thing works fine I have the object added and the datatable updated:
public void createusr(ActionEvent event) {
UserDTO usr = new UserDTO();
listusr.add(0, usr);
}
and here is the onCancel listener:
public void onCancel(RowEditEvent event){
list.remove(0);
}
The listener is fired and the object is deleted by I the datatable is not updated and instead of deleting the row I have the second row duplicated.
listusr.remove(0)
instead oflist.remove(0)
? – Mr.J4mes