Editable DataTable again - inserting row
petercr4 Feb 25, 2008 9:38 AMI would like to make a simple editable datatable with Update, Delete and Insert operations. I had no problem with Update and Delete parts.
For updating I just turned OutputText into InputText, for deleting I have a button which calls delete method on server.
What would be the right approach for inserting? I would like first to insert record only on the client side in datatable, let the user complete data, and then save it back. The problem is I don't know how to insert an empty row into DataTable.
Thanks for answer.
session bean part
...
@DataModel private List<SmOrganizations> organizations; @Factory("organizations") public void getOrganizations() { organizations = em.createQuery("select o from SmOrganizations o order by idOrganization desc").getResultList(); } public void delete() { System.out.println("deleting..."); SmOrganizations toDelete = em.merge(selectedOrganization); em.remove(toDelete); getOrganizations(); } // this is not good, shouldn't be on database immediatelly public void addOrganization(ActionEvent event) { System.out.println("addOrg called"); newOrganization = new SmOrganizations(); organizations.add(newOrganization); //em.persist(newOrganization); //getOrganizations(); }
xhtml part
...
<a4j:form styleClass="richInput"> <h:commandLink action="#{organizationManager.putInto}" value="Save" /> <h:commandLink actionListener="#{organizationManager.addOrganization}" value="New..." /> <rich:spacer height="30" /> <rich:datascroller align="left" for="organizations" maxPages="20" /> <rich:spacer height="30" /> <rich:dataTable width="483" id="organizations" columnClasses="col" rows="10" value="#{organizations}" var="org" styleClass="richInput"> <f:facet name="header"> <rich:columnGroup> <h:column> <h:outputText styleClass="headerText" value="Id" /> </h:column> <h:column> <h:outputText styleClass="richColumn" value="Name1" /> </h:column> <h:column> <h:outputText styleClass="headerText" value="Address1" /> </h:column> </rich:columnGroup> </f:facet> <h:column> <h:outputText value="#{org.idOrganization}" styleClass="richInput" width="100%" /> </h:column> <h:column> <h:inputText value="#{org.name1}" styleClass="richInput" width="100%" /> </h:column> <h:column> <h:inputText value="#{org.address1}" styleClass="richInput" /> </h:column> <h:column> <h:commandButton value="Delete..." reRender="organizations" action="#{organizationManager.delete}"/> </h:column> </rich:dataTable> </a4j:form>