2 Replies Latest reply on Mar 3, 2008 3:39 AM by Peter ÄŒebokli

    Editable DataTable again - inserting row

    Peter ÄŒebokli Newbie

      I 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>
      


        • 1. Re: Editable DataTable again - inserting row
          scott duke Novice

          I was able to do what you are wanting from help on another forum. There is probably another way to do this, but they advised creating a RowBean class to maintain the information. Here is what my RowBean class looked like:

          public class RowBean
          {
           private Object object;
           private Object objectBackup;
           private boolean selected = false;
           private boolean editable = false;
           private boolean adding = false;
          
           public RowBean(Object object)
           {
           this.object = object;
           this.objectBackup = object;
           }
          
           public boolean isSelected()
           {
           return selected;
           }
          
           public void setSelected(boolean selected)
           {
           this.selected = selected;
           }
          
           public boolean isEditable()
           {
           return editable;
           }
          
           public void setEditable(boolean editable)
           {
           this.editable = editable;
           }
          
           public boolean isAdding()
           {
           return adding;
           }
          
           public void setAdding(boolean adding)
           {
           this.adding = adding;
           }
          
           public Object getObject()
           {
           return object;
           }
          
           public void setObject(Object object)
           {
           this.object = object;
           this.objectBackup = object;
           }
          
           public Object getObjectBackup()
           {
           return objectBackup;
           }
          
           public void setObjectBackup(Object object)
           {
           this.objectBackup = object;
           }
          
           public void editRow(ActionEvent event)
           {
           //System.out.println("edit() called for row: " + this.id );
           editable = true;
           }
           public void saveRow(ActionEvent event)
           {
           //System.out.println("save() called for row: " + this.id );
           editable = false;
           adding = false;
           }
          }
          


          I used mine for multiple addresses. On my backing bean I used something like this to add a row:

           public void addRow(ActionEvent event)
           {
           List <RowBean> list = (List <RowBean>)dataModel.getWrappedData();
           if (list.size() == newAddress.MAX_ALLOWED)
           {
           confirmingMaxAllowed = true;
           }
           else
           {
           log.info("Adding new address.");
           newCityStateZip = new CityStateZip();
           newAddress = new Address();
           newAddress.setCityStateZip(newCityStateZip);
           newRowBean = new RowBean(newAddress);
           newRowBean.setEditable(true);
           newRowBean.setAdding(true);
           list.add(0, newRowBean);
           }
           }
          


          This will always place the new row as the first row on your table. Validation is another story.

          The only problem with using this is there is a lot of maintenance code needed. For me, I wanted to maintain the information in memory and one save it to the database when the user pressed "save" and discard the information if they pressed "cancel". This added a lot of extra coding. If you stored it directly to the database, it would be a lot easier.

          I hope this helps. By the way, I am using ICEFaces and their forum is where I received the help.

          • 2. Re: Editable DataTable again - inserting row
            Peter ÄŒebokli Newbie

            Thank you sandman202. I'll try with your approach and check ICEFaces too.