title: Primefaces p:datatable adding and editing rows not working
body: <p>I'm having problems with relatively simple JSF2 site with Primefaces. I'm using Primefaces 3.5 and WebSphere Application Server v7.0</p>
  
  <p>I have a page with a p:datatable which displays documents. I would like to be able to add, edit and delete said documents with a confirm/input -dialog. But when I click the Add document -button, input the name for the document and click Save, only an empty row apperas to the table. When I click the edit button and give it a proper name it is saved accordingly. But if I try to edit another row, the dialog displays the last document's name I edited.</p>
  
  <p>There might be something wrong with the references, but I can't wrap my head around it. I have tried to research the subject but whit no avail. When a new document is added(or edited) the setter is called twice so I wonder if it has anything to do with this <a href="http://code.google.com/p/primefaces/issues/detail?id=4681" rel="nofollow">http://code.google.com/p/primefaces/issues/detail?id=4681</a></p>
  
  <p>Here's the code-files:</p>
  
  <p>test.xhtml</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui"&gt;
  &lt;h:head&gt;
  &lt;title&gt;test&lt;/title&gt;
  &lt;meta http-equiv="Content-Type"
      content="application/xhtml+xml; charset=UTF-8" /&gt;
  &lt;/h:head&gt;
  &lt;h:body&gt;
  &lt;h:form&gt;
  
      &lt;p:dataTable var="document" value="#{testMB.list}" id="documentTable"
          paginator="true" rows="15" paginatorPosition="bottom"
          paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
          emptyMessage="No documents"&gt;
          &lt;f:facet name="header"&gt;
              List of documents
          &lt;/f:facet&gt;
          &lt;p:column headerText="Document"&gt;
              &lt;h:outputText value="#{document.name}" /&gt;
          &lt;/p:column&gt;
          &lt;p:column width="70"&gt;
              &lt;p:commandButton icon="ui-icon-trash"
                  oncomplete="deleteDocumentDlg.show()"
                  style="float:right;width:32px;height:32px;top-margin:0px;bottom-margin:0px;top-padding:0px;bottom-padding:0px;"&gt;
                  &lt;f:setPropertyActionListener value="#{document}"
                      target="#{testMB.document}" /&gt;
              &lt;/p:commandButton&gt;
  
              &lt;p:commandButton icon="ui-icon-pencil"
                  oncomplete="editDocumentDlg.show()"
                  style="float:right;width:32px;height:32px;top-margin:0px;bottom-margin:0px;top-padding:0px;bottom-padding:0px;"
                  action="#{testMB.saveChangesToDocument}"&gt;
                  &lt;f:setPropertyActionListener value="#{document}"
                      target="#{testMB.document}" /&gt;
              &lt;/p:commandButton&gt;
  
          &lt;/p:column&gt;
  
      &lt;/p:dataTable&gt;
  
      &lt;p:commandButton value="New document"
          oncomplete="newDocumentDlg.show()" style="float:right" /&gt;
  
  
  
      &lt;!--    Add new document -dialog --&gt;
      &lt;p:dialog widgetVar="newDocumentDlg" id="newDocumentDialog"
          header="Add new document" message="Add new document"
          hideEffect="fade" showEffect="fade" resizable="true" closable="true"
          lazy="true" width="600"&gt;
  
          &lt;h:panelGrid columns="3"&gt;
              &lt;h:outputText value="Name" /&gt;
              &lt;p:spacer width="10" height="10" /&gt;
              &lt;p:inputText value="#{testMB.document.name}" width="35"
                  maxlength="128" /&gt;
          &lt;/h:panelGrid&gt;
  
          &lt;br /&gt;
  
  
          &lt;h:panelGroup layout="block" style="text-align: right"&gt;
              &lt;p:commandButton value="Save" action="#{testMB.addDocument}"
                  oncomplete="newDocumentDlg.hide();" update="documentTable"&gt;
              &lt;/p:commandButton&gt;
              &lt;p:commandButton value="Cancel" oncomplete="newDocumentDlg.hide();"
                  action="#{testMB.cancelAction}" /&gt;
          &lt;/h:panelGroup&gt;
  
      &lt;/p:dialog&gt;
  
  
  
      &lt;!--    Delete document -dialog --&gt;
      &lt;p:dialog widgetVar="deleteDocumentDlg" id="deleteDocumentDialog"
          header="Delete document" message="Delete document" hideEffect="fade"
          showEffect="fade" resizable="false" closable="true" lazy="true"&gt;
  
          &lt;h:outputText value="Delete document?" /&gt;
  
          &lt;h:panelGroup layout="block" style="text-align: right"&gt;
              &lt;p:commandButton value="Delete" action="#{testMB.deleteDocument}"
                  oncomplete="deleteDocumentDlg.hide();" update="documentTable"&gt;
              &lt;/p:commandButton&gt;
              &lt;p:commandButton value="Cancel"
                  oncomplete="deleteDocumentDlg.hide();"
                  action="#{testMB.cancelAction}" /&gt;
          &lt;/h:panelGroup&gt;
  
      &lt;/p:dialog&gt;
  
  
      &lt;p:dialog widgetVar="editDocumentDlg" id="editDocumentDialog"
          header="Edit document" message="Edit document" hideEffect="fade"
          showEffect="fade" resizable="true" closable="true" lazy="true"
          width="600"&gt;
  
          &lt;h:panelGrid columns="3"&gt;
              &lt;h:outputText value="Name" /&gt;
              &lt;p:spacer width="10" height="10" /&gt;
              &lt;p:inputText value="#{testMB.document.name}" width="35"
                  maxlength="128" /&gt;
          &lt;/h:panelGrid&gt;
  
          &lt;br /&gt;
  
          &lt;h:panelGroup layout="block" style="text-align: right"&gt;
              &lt;p:commandButton value="Save" action="#{testMB.saveChanges}"
                  oncomplete="editDocumentDlg.hide();" update="documentTable"&gt;
              &lt;/p:commandButton&gt;
              &lt;p:commandButton value="Cancel" oncomplete="editDocumentDlg.hide();"
                  action="#{testMB.cancelAction}" /&gt;
          &lt;/h:panelGroup&gt;
  
      &lt;/p:dialog&gt;
  
  &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>testMB.java</p>
  
  <pre class="lang-java prettyprint-override"><code>import Document
  
  @ManagedBean
  @ViewScoped
  public class testMB implements Serializable {
  
      private Document document;
  
      ArrayList&lt;Document&gt; list;
  
      @PostConstruct
      public void alusta() {
          System.err.println("POSTCONSTRUCT");
  
          document = new Document();
          list = new ArrayList();
      }
  
      public String addDocument() {
          System.err.println("Adding new document: " + document.getName());
  
          list.add(document);
          document = new Document();
  
          return null;
      }
  
      public String deleteDocument() {
          System.err.println("Deleting document: " + document.getName());
          list.remove(document);
          document = new Document();
          return null;
      }
  
      public String saveChanges() {
          System.err.println("Saving changes to document: " + document.getName());
          list.get(list.indexOf(document)).setName(document.getName());
          document = new Document();
          return null;
      }
  
      public String cancelAction() {
          document = new Document();
          return null;
      }
  
      public void setDocument(Document document) {
          System.err.println("Document set to: " + document.getName());
          this.document = document;
      }
  
      public Document getDocument() {
          return document;
      }
  
      public void setList(ArrayList&lt;Document&gt; list) {
          this.list = list;
      }
  
      public ArrayList&lt;Document&gt; getList() {
          return list;
      }
  }
  </code></pre>
  
  <p>Document.java</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Document {
  
      private String name;
  
      public Document() {
          System.out.println("Document's constructor");
          name = null;
      }
  
      public Document(Document src) {
          System.out.println("Document's copy constructor");
          this.name = src.name;
      }
  
      public void setName(String name) {
          System.err.println("Name set to " + name);
          this.name = name;
      }
  
      public String getName() {
          return name;
      }
  
  }
  </code></pre>
  
  <p>I'd be grateful for your feedback.</p>