2013-08-07 36 views
1

我遇到了與Primefaces相對簡單的JSF2網站的問題。我正在使用Primefaces 3.5和WebSphere Application Server v7.0Primefaces p:datatable添加和編輯行不起作用

我有一個頁面,其中包含顯示文檔的p:datatable。我希望能夠使用confirm/input -dialog添加,編輯和刪除所述文檔。但是,當我點擊添加文檔按鈕時,輸入文檔的名稱並單擊保存,只有一個空行出現在表格中。當我點擊編輯按鈕並給它一個合適的名字時,它就會相應地保存。但是如果我嘗試編輯另一行,對話框將顯示我編輯的最後一個文檔的名稱。

參考文獻可能有問題,但我無法將其包裹在它的周圍。我試圖研究這個問題,但惠威無濟於事。當添加一個新的文檔(或修改),所以我不知道設置器調用兩次,如果有任何與此http://code.google.com/p/primefaces/issues/detail?id=4681

這裏的代碼文件:

test.xhtml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<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"> 
<h:head> 
<title>test</title> 
<meta http-equiv="Content-Type" 
    content="application/xhtml+xml; charset=UTF-8" /> 
</h:head> 
<h:body> 
<h:form> 

    <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"> 
     <f:facet name="header"> 
      List of documents 
     </f:facet> 
     <p:column headerText="Document"> 
      <h:outputText value="#{document.name}" /> 
     </p:column> 
     <p:column width="70"> 
      <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;"> 
       <f:setPropertyActionListener value="#{document}" 
        target="#{testMB.document}" /> 
      </p:commandButton> 

      <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}"> 
       <f:setPropertyActionListener value="#{document}" 
        target="#{testMB.document}" /> 
      </p:commandButton> 

     </p:column> 

    </p:dataTable> 

    <p:commandButton value="New document" 
     oncomplete="newDocumentDlg.show()" style="float:right" /> 



    <!-- Add new document -dialog --> 
    <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"> 

     <h:panelGrid columns="3"> 
      <h:outputText value="Name" /> 
      <p:spacer width="10" height="10" /> 
      <p:inputText value="#{testMB.document.name}" width="35" 
       maxlength="128" /> 
     </h:panelGrid> 

     <br /> 


     <h:panelGroup layout="block" style="text-align: right"> 
      <p:commandButton value="Save" action="#{testMB.addDocument}" 
       oncomplete="newDocumentDlg.hide();" update="documentTable"> 
      </p:commandButton> 
      <p:commandButton value="Cancel" oncomplete="newDocumentDlg.hide();" 
       action="#{testMB.cancelAction}" /> 
     </h:panelGroup> 

    </p:dialog> 



    <!-- Delete document -dialog --> 
    <p:dialog widgetVar="deleteDocumentDlg" id="deleteDocumentDialog" 
     header="Delete document" message="Delete document" hideEffect="fade" 
     showEffect="fade" resizable="false" closable="true" lazy="true"> 

     <h:outputText value="Delete document?" /> 

     <h:panelGroup layout="block" style="text-align: right"> 
      <p:commandButton value="Delete" action="#{testMB.deleteDocument}" 
       oncomplete="deleteDocumentDlg.hide();" update="documentTable"> 
      </p:commandButton> 
      <p:commandButton value="Cancel" 
       oncomplete="deleteDocumentDlg.hide();" 
       action="#{testMB.cancelAction}" /> 
     </h:panelGroup> 

    </p:dialog> 


    <p:dialog widgetVar="editDocumentDlg" id="editDocumentDialog" 
     header="Edit document" message="Edit document" hideEffect="fade" 
     showEffect="fade" resizable="true" closable="true" lazy="true" 
     width="600"> 

     <h:panelGrid columns="3"> 
      <h:outputText value="Name" /> 
      <p:spacer width="10" height="10" /> 
      <p:inputText value="#{testMB.document.name}" width="35" 
       maxlength="128" /> 
     </h:panelGrid> 

     <br /> 

     <h:panelGroup layout="block" style="text-align: right"> 
      <p:commandButton value="Save" action="#{testMB.saveChanges}" 
       oncomplete="editDocumentDlg.hide();" update="documentTable"> 
      </p:commandButton> 
      <p:commandButton value="Cancel" oncomplete="editDocumentDlg.hide();" 
       action="#{testMB.cancelAction}" /> 
     </h:panelGroup> 

    </p:dialog> 

</h:form> 
</h:body> 
</html> 

testMB.java

import Document 

@ManagedBean 
@ViewScoped 
public class testMB implements Serializable { 

    private Document document; 

    ArrayList<Document> 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<Document> list) { 
     this.list = list; 
    } 

    public ArrayList<Document> getList() { 
     return list; 
    } 
} 

Document.java

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; 
    } 

} 

我很感謝您的反饋。

回答

0

您的代碼有一些問題。但我能看到的問題是

<!-- Add new document -dialog --> 
<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"> 

    <h:panelGrid columns="3"> 
     <h:outputText value="Name" /> 
     <p:spacer width="10" height="10" /> 
     <p:inputText value="#{testMB.document.name}" width="35" 
      maxlength="128" /> 
    </h:panelGrid> 

您無法將輸入文本設置爲當前文檔名稱。

而是使用另一個變量爲新的文件名保存值一樣

private String newName; 

public String getNewName() { 
    return newName; 
} 

public void setNewName(String newName) { 
    this.newName = newName; 
} 

然後改變你的JSPX

<h:panelGrid columns="3"> 
      <h:outputText value="Name" /> 
      <p:spacer width="10" height="10" /> 
      <p:inputText value="#{testMB.newName}" width="35" 
       maxlength="128" /> 
     </h:panelGrid> 

那麼你addDocument()成爲

document.setName(newName); 
    documentList.add(document); 
    document = new Document(); 

我希望幫助。

+0

感謝您的反饋。你的回答幫助我開始,但仍然有一些古怪。當我添加一行時,一切正常,但當我嘗試添加另一行時,對話框的inputText組件顯示第一行的名稱。即使在將該行添加到表格後將其設置爲「」。 但是,無論如何,這肯定幫了我很多。 –

+0

如果它有幫助,然後投票並接受答案。 – Makky

+0

我很抱歉無法投票,因爲我的名聲低於15,這似乎是極限。 –