I want make CRUD PrimeFaces dataTable with add, filter, edit, delete, pagination features.
I am trying to ‘Add’ a new record in a dialogue (i.e. modal pop-up window) using PrimeFaces 3.4.2 dataTable, JSF 2.1.
All other operations are working good but the dataTable is not updating correctly after an add operation.
It is updated correctly after the 1st add operation.
After that I add 2nd row the dataTable is updated but the row which was added first is replaced with the row which was added 2nd time.
After that I add a 3rd row, the 1st row has got replaced with the 3rd row.
The database tables are getting updated perfect in all the cases.
Code snippet from jsf and managed bean :
JSF code:
<h:body>
<h:form id="form1">
<h:messages id="messages1"/>
<br/>
<p:commandButton id="Addbtn" value="Add" oncomplete="AddDialog.show()" update=":addform"/>
<br/>
<p:dataTable id="dtusers" value="#{userdataController.retrieveData()}" var="item"
rows="8" paginator="true" emptyMessage="No Records Found"
paginatorAlwaysVisible="false">
<p:column style="width:150px" filterMatchMode="contains" filterBy ="#{item.id}">
<f:facet name="header"> Id </f:facet>
<h:outputText value="#{item.id}"/>
</p:column>
<p:column style="width:150px" filterMatchMode="contains" filterBy="#{item.name}">
<f:facet name="header"> Name </f:facet>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column style="width:150px">
<p:commandButton id="editButton" update=":editform" oncomplete="EditDialog.show()" value="Edit">
<f:setPropertyActionListener value="#{item}" target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
<p:column style="width:150px">
<p:commandButton id="deleteButton" update=":deleteform" oncomplete="UserDialog.show()" value="Delete">
<f:setPropertyActionListener value="#{item}" target="#{userdataController.selectedUser}"/>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog header="Add User Data" resizable="false" id="AddDialog" widgetVar="AddDialog" modal="true" showEffect="fade" hideEffect="fade" position="center" dynamic="true">
<h:form id="addform">
<p:outputPanel id="AddDisplay">
<p:panelGrid columns="2" >
<f:facet name="header">
Add Userdata
</f:facet>
<h:outputLabel for="Id" value="Enter Id:"/>
<p:inputText id="Id" value="#{userdataController.selected.id}" />
<h:outputLabel for="Name" value="Enter Name:"/>
<p:inputText id="Name" value="#{userdataController.selected.name}"/>
<p:commandButton actionListener="#{userdataController.create}" update=":form1:dtusers :form1:messages1" value="Save" oncomplete="AddDialog.hide()" style="margin:0"/>
</p:panelGrid>
</p:outputPanel>
</h:form>
</p:dialog>
</h:body>
Manaed Bean code snippet:
@ManagedBean(name = "userdataController")
@SessionScoped
public class UserdataController implements Serializable {
private Userdata current;
private DataModel items = null;
@EJB
private PrimeFacesDb.session.UserdataFacade ejbFacade;
private int id;
private String name;
private Userdata SelectedUser;
public UserdataController() {
}
//getters setters for id,name and SelectedUser
private UserdataFacade getFacade() {
return ejbFacade;
}
public List retrieveData() {
List result = getFacade().findAll();
return result;
}
public void create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage("Userdata created successfully");
} catch (Exception e) {
JsfUtil.addErrorMessage(e, "Persistence Error occured");
}
}
}
The information available in PrimeFaces forum, NetBeans Sample code and Editable datable sample(mastertheboss.com/primefaces/datatables-with-primefaces/…;) helped me to create a better demonstrable CRUD dataTable with create,filter,edit and delete features. It would be incomplete without mentioning Mr.BalusC because his comments,solutions on JSF 2.x posts are valuable to me.
@PostConstruct annotation is used for init() method, based on NetBeans Sample Application ScrumToys.
@PostConstruct
public void init() {
current = new Users();
}
The information PrimeFaces forum is also helped me to identify the problem in java code in the Model.