簡體   English   中英

Primefaces p:數據表添加和編輯行不起作用

[英]Primefaces p:datatable adding and editing rows not working

我在使用Primefaces的相對簡單的JSF2站點時遇到問題。 我正在使用Primefaces 3.5和WebSphere Application Server v7.0

我有一個帶有ap:datatable的頁面,用於顯示文檔。 我希望能夠使用Confirm / input -dialog添加,編輯和刪除所述文檔。 但是,當我單擊“添加文檔”按鈕時,輸入文檔的名稱並單擊“保存”,表中只有空行apperas。 當我單擊編輯按鈕並給它起一個適當的名稱時,它會相應保存。 但是,如果我嘗試編輯另一行,則對話框會顯示我編輯的最后一個文檔的名稱。

這些引用可能有問題,但是我無法解決。 我已經嘗試研究這個主題,但是沒有用。 當添加(或編輯)新文檔時,設置程序將被調用兩次,所以我想知道它是否與此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;
    }

}

感謝您的反饋。

您的代碼有一些問題。 但是我看到的問題在這里

<!--    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();

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM