PrimeFaces dataTable在Ajax请求后没有更新

时间:2011-08-10 15:37:03

标签: jsf primefaces

我目前正在使用PrimeFaces dataTable,并且在用户添加后立即无休止地向dataTable追加新内容。服务器端,一切正常 - 数据保存到数据库和硬盘。我希望在成功请求后看到部分页面更新,而我似乎无法让它发挥作用。这是我的XHTML代码:

<div class="content">
        <div id="mainContent">

            <div class="topNav">
                <h:form id="navForm">
                    <p:commandButton value="Upload"/>
                    <p:commandButton value="New Folder" onclick="createDlg.show();" />
                    <p:commandButton value="Delete Folder" action="#" />
                </h:form>
            </div>

            <div id="filePanel">
                <h:form id="dataTable" prependId="false">     
                    <p:dialog header="Create" widgetVar="createDlg" modal="true" height="200">

                        <p:panel header="New Folder">
                            <h:panelGrid columns="2">
                                <h:outputLabel value="Name:" for="txt_name"></h:outputLabel>
                                <p:inputText id="txt_name" value="#{directoryController.newDir.name}" required="true" />
                                <p:commandButton value="Create" actionListener="#{directoryController.createNew}" update="dGrid" oncomplete="createDlg.hide();"/>
                                <p:ajaxStatus style="width:16px; height:16px;">
                                    <f:facet name="start">
                                        <h:outputText value="Saving..." />
                                    </f:facet>
                                    <f:facet name="">
                                        <h:outputText value="Complete." />
                                    </f:facet>
                                </p:ajaxStatus>
                            </h:panelGrid>
                        </p:panel>

                    </p:dialog>
                    <p:dataTable id="dGrid" value="#{directoryController.itemsByOwner}" var="dir">
                        <p:column selectionMode="multiple"/>
                        <p:column sortBy="#{dir.name}">                                
                            <f:facet name="header">
                                File Name
                            </f:facet>
                            <h:outputText value="#{dir.name}" />
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                File Size
                            </f:facet>
                        </p:column>
                    </p:dataTable>                                
                </h:form>                    
            </div>
        </div>
    </div>

最后,这是我的支持bean。

public String createNew(){
    newDir.setDateCreated(new Date());
    newDir.setDateModified(new Date());
    newDir.setId(BigDecimal.ZERO);
    newDir.setLocation(ROOT_DIRECTORY + currentUser.getUsername() + "/");
    newDir.setFilesCollection(f);
    newDir.setDescription("");
    newDir.setOwner(currentUser);
    current = newDir;
    create();
    persistDirectoryFromObject(newDir);
    newDir = new Directory();
    RequestContext.getCurrentInstance().addPartialUpdateTarget("dGrid");


    return null;
}

1 个答案:

答案 0 :(得分:1)

如果你使用3.0.M4,你可以这样做:

...         ...

<p:outputPanel autoUpdate="true">

                <p:dataTable id="dGrid" value="#{directoryController.itemsByOwner}" var="dir">
                    <p:column selectionMode="multiple"/>
                    <p:column sortBy="#{dir.name}">                                
                        <f:facet name="header">
                            File Name
                        </f:facet>
                        <h:outputText value="#{dir.name}" />
                    </p:column>
                    <p:column>
                        <f:facet name="header">
                            File Size
                        </f:facet>
                    </p:column>
                </p:dataTable>    
</p:outputPanel>

或者你可以简单地说:

<p:commandButton value="Create" action="#{directoryController.createNew}" oncomplete="createDlg.hide();" ajax="false" />