【问题标题】:Primefaces p:datatable adding and editing rows not workingPrimefaces p:datatable添加和编辑行不起作用
【发布时间】:2013-08-07 06:22:05
【问题描述】:

我在使用 Primefaces 的相对简单的 JSF2 站点时遇到问题。我正在使用 Primefaces 3.5 和 WebSphere Application Server v7.0

我有一个带有 p:datatable 的页面,它显示文档。我希望能够使用确认/输入对话框添加、编辑和删除所述文档。但是当我单击“添加文档”按钮时,输入文档的名称并单击“保存”,表格中只有一个空行。当我单击编辑按钮并为其命名时,它会相应地保存。但如果我尝试编辑另一行,对话框会显示我最后编辑的文档名称。

引用可能有问题,但我无法理解它。我曾尝试研究该主题,但无济于事。当添加(或编辑)新文档时,setter 会被调用两次,所以我想知道它是否与 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;
    }
}

文档.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;
    }

}

非常感谢您的反馈。

【问题讨论】:

    标签: java jsf-2 primefaces


    【解决方案1】:

    您的代码有一些问题。但是我能看到的问题就在这里

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

    我希望这会有所帮助。

    【讨论】:

    • 感谢您的反馈。您的回答帮助我入门,但仍有一些奇怪之处。当我添加一行时,一切正常,但是当我尝试添加另一行时,对话框的 inputText 组件显示第一行的名称。即使在将行添加到表后将其设置为“”。但无论如何,这确实对我有很大帮助。
    • 很抱歉无法投票,因为我的声誉低于 15 岁,这似乎是上限。
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    相关资源
    最近更新 更多