在Ajax请求上重置Primefaces Pagination Selected Page

时间:2011-04-11 05:03:27

标签: primefaces

我不确定这是一个错误还是只是未实现但我无法更新dataGrids页面属性以将分页重置为第1页。我已将它绑定到我的bean中的表达式并通过ajax更新更新它,但单击我的按钮时它不会更新。分页器将保留在选定页面上,不会通过ajax请求重置。我正在尝试重置它。 dreamSearchBean的setCurrentPage会被调用并被传递1,但它会保留在上次选择的任何页面上

<h:form id="dreamWebSearchFrm">
<p:commandButton styleClass="form-btn1" value="#{bundle['dreamSearch.search.button.TEXT']}" onclick="trackingDreamSearch()"
        actionListener="#{dreamSearch.search}" update=":dreamWebSearchFrm:resultsPnl">
        <f:setPropertyActionListener value="1" target="#{dreamSearchBean.currentPage}"/>    
</p:commandButton>
<p:panel id="resultsPnl">
                    <div class="data-grid-wrap">
                    <h:outputFormat escape="false" value="#{bundle['dreamSearch.imageResults.TEXT']}" rendered="#{dreamSearchBean.shouldRender}" >
                        <f:param value="#{dreamSearchBean.searchText}" />
                    </h:outputFormat>
                        <p:dataGrid var="dream" value="#{dreamSearchBean.dreams}" rendered="#{dreamSearchBean.shouldRender}" page="#{dreamSearchBean.currentPage}" pageLinks="3"  columns="4" rows="4" paginator="true" effect="true"
                            styleClass="ui-header-visibility"
                            paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}"
                            paginatorPosition="bottom">
                            <p:column>
                                <h:panelGrid columns="1">
                                    <p:commandLink onclick="webSearchDlg.hide();dreamEditDlg.show();" update=":dreamEditFrm:display"> 
                                        <f:setPropertyActionListener value="#{dream}" target="#{dreamModifyBean.selectedDream}"/>
                                        <p:graphicImage value="#{dream.imageThumb}" width="125" height="100"></p:graphicImage>
                                    </p:commandLink>
                                </h:panelGrid>
                            </p:column>
                        </p:dataGrid>
                    </div>
                </p:panel>
</h:form>

6 个答案:

答案 0 :(得分:5)

使用Primefaces 3.2当我进行查询时遇到类似的问题,当我得到例如五页时,我会进行新的查询,页面计数将为10,但它将默认为上一个查询并保持第5页。重置为第一页的解决方案如下:

DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("documentSearch:searchFormCase:documentSearchTable");
dataTable.setFirst(0);

即使我正在使用:

import org.primefaces.component.datatable.DataTable;

它应该适用于以下组件:

import org.primefaces.component.datagrid.DataGrid;

答案 1 :(得分:3)

您必须扩展Primefaces的DataTableRenderer。创建一个新类,也许它可以是扩展DataTableRenderer的DataTableRendererExt。

覆盖encodeMarkup(...)方法,复制原始代码,然后插入对resetPagination()的调用:

@Override
protected void encodeMarkup(FacesContext context, DataTable table) throws IOException{
    ResponseWriter writer = context.getResponseWriter();
   ...
    if(hasPaginator) {
        table.resetPagination();
        table.calculatePage();
    }

要在应用程序中绑定此扩展类,必须在faces-config.xml中插入此块:

<render-kit>
    <renderer>
            <component-family>org.primefaces.component</component-family>
            <renderer-type>org.primefaces.component.DataTableRenderer</renderer-type>
            <renderer-class>your.package.DataTableRendererExt</renderer-class>
    </renderer>
</render-kit>

DataTableRenderer还有另一个错误 - lazyLoading。数据表在第一次加载时为空,因为table.loadLazyData()方法中encodeTbody(...)的放置不正确。

以下是扩展类的代码:

public class DataTableRendererExt extends DataTableRenderer
{

    @Override
    protected void encodeMarkup(FacesContext context, DataTable table) throws IOException{
        ResponseWriter writer = context.getResponseWriter();
        String clientId = table.getClientId(context);
        boolean scrollable = table.isScrollable();
        String containerClass = scrollable ? DataTable.CONTAINER_CLASS + " " + DataTable.SCROLLABLE_CONTAINER_CLASS : DataTable.CONTAINER_CLASS;
        containerClass = table.getStyleClass() != null ? containerClass + " " + table.getStyleClass() : containerClass;
        String style = null;
        boolean hasPaginator = table.isPaginator();
        String paginatorPosition = table.getPaginatorPosition();

        if(hasPaginator) {
            table.resetPagination();
            table.calculatePage();
        }

        writer.startElement("div", table);
        writer.writeAttribute("id", clientId, "id");
        writer.writeAttribute("class", containerClass, "styleClass");
        if((style = table.getStyle()) != null) {
            writer.writeAttribute("style", style, "style");
        }

        encodeFacet(context, table, table.getHeader(), DataTable.HEADER_CLASS);

        if(hasPaginator && !paginatorPosition.equalsIgnoreCase("bottom")) {
            encodePaginatorMarkup(context, table, "top");
        }

        if(scrollable) {
            encodeScrollableTable(context, table);

        } else {
            encodeRegularTable(context, table);
        }

        if(hasPaginator && !paginatorPosition.equalsIgnoreCase("top")) {
            encodePaginatorMarkup(context, table, "bottom");
        }

        encodeFacet(context, table, table.getFooter(), DataTable.FOOTER_CLASS);

        if(table.isSelectionEnabled()) {
            encodeSelectionHolder(context, table);
        }

        writer.endElement("div");
    }

    /**
     * @see org.primefaces.component.datatable.DataTableRenderer#encodeTbody(javax.faces.context.FacesContext, org.primefaces.component.datatable.DataTable)
     * Fix for lazy load bug: data table is empty on very first load because of wrong palcement of table.loadLazyData();
     */
    @Override
    protected void encodeTbody(FacesContext context, DataTable table) throws IOException {
        ResponseWriter writer = context.getResponseWriter();
        String rowIndexVar = table.getRowIndexVar();
        String clientId = table.getClientId(context);
        String emptyMessage = table.getEmptyMessage();
        String selectionMode = table.getSelectionMode();
        String columnSelectionMode = table.getColumnSelectionMode();
        String selMode = selectionMode != null ? selectionMode : columnSelectionMode != null ? columnSelectionMode : null;
        Object selection = table.getSelection();


        if(table.isLazy()) {
            table.loadLazyData();
        }

        int rows = table.getRows();
        int first = table.getFirst();
        int rowCount = table.getRowCount();
        int rowCountToRender = rows == 0 ? rowCount : rows;
        boolean hasData = rowCount > 0;



        String tbodyClass = hasData ? DataTable.DATA_CLASS : DataTable.EMPTY_DATA_CLASS;

        writer.startElement("tbody", null);
        writer.writeAttribute("id", clientId + "_data", null);
        writer.writeAttribute("class", tbodyClass, null);

        if(hasData) {
            if(selectionMode != null && selection != null) {
                handlePreselection(table, selectionMode, selection);
            }

            for(int i = first; i < (first + rowCountToRender); i++) {
                encodeRow(context, table, clientId, i, rowIndexVar);
            }
        }
        else if(emptyMessage != null){
            //Empty message
            writer.startElement("tr", null);
            writer.writeAttribute("class", DataTable.ROW_CLASS, null);

            writer.startElement("td", null);
            writer.writeAttribute("colspan", table.getColumns().size(), null);
            writer.write("&nbsp;");
            writer.endElement("td");

            writer.endElement("tr");
        }

        writer.endElement("tbody");

        //Cleanup
        table.setRowIndex(-1);
        if(rowIndexVar != null) {
            context.getExternalContext().getRequestMap().remove(rowIndexVar);
        }
    }
}

答案 2 :(得分:1)

我做过类似的事情(对于primefaces 3.4.2)

widgetVar.getPaginator().setPage(0)

和我的按钮

<p:commandButton value="Search" update=":tableForm:dataTable" onclick="dataTable.getPaginator().setPage(0)"/>

和我的表

<h:form id="tableForm">  
<p:dataTable id="dataTable" var="..." value="#{...}"  widgetVar="dataTable" ...

答案 3 :(得分:0)

我从未使用它,但在Primefaces文档中,page有一个属性p:dataGrid。描述如下:

  

当前页面的索引,第一页   是1(类型:整数,默认值:1)

也许您可以使用el表达式并在启动新搜索时重置辅助bean中的值:

<p:dataGrid var="dream" value="#{dreamSearchBean.dreams}"
            ...
            paginatorPosition="bottom"
            page=#{dreamSearchBean.currentPage}>

答案 4 :(得分:0)

您的问题可以通过与我的情况相同的解决方案解决(我想完全重置dataTable):

How to restore the state of p:dataTable before dialog reopen?

您需要声明组件绑定,并且您将在bean中设置DataTable:

private DataTable tableBinding;

现在你可以调用你想要的任何操作,这在JavaScript API中是缺失的(我认为它应该可用于JavaScript API,但我们对此没有影响)。

答案 5 :(得分:0)

在PF 4.0中我使用此

DataGrid dataGrid = (DataGrid) FacesContext.getCurrentInstance().getViewRoot().findComponent("formEventList:eventsList");
dataGrid.setFirst(0);

DataGrid没有像DataTable那样的reset()函数。