简体   繁体   中英

How to Refresh Datatable Dynamic Columns After Pagination

I use primefaces 3.1 and I have a datatable that shows paginated data, whose columns are constructed based on the data of the page and rendered via <p:columns/> tag. With this approach, different pages of the same datatable can have different columns. I tought it would re-render columns on pagination by default, but it doesn't. Ive tried <p:ajax event="page" update="myTable"/> and <p:ajax event="page" update="myTableContainer"/> with any luck.

Is there a way to re-render the whole datatable when pagination is triggered, so not only content but column headers are showed properly?

Edit: Heres some code sample as you requested.

Ive made my own implementation of LazyDataModel and extended it overriding the load method implementation:

@Override
public List<StockItem> load(int i, int i1, String string, SortOrder sortOrder, Map<String, String> map) {
    List<StockItem> stockItems = super.load(i, i1, string, sortOrder, map);
    inventoryMovements = service.listInventoryMovements(stockItems, fromDate, toDate);
    stockOperations = new HashSet<StockOperation>();
    quantityTypes = new HashSet<QuantityType>();
    for (Map.Entry<StockItem, ItemMovements> entry : inventoryMovements.entrySet()) {
        quantityTypes.addAll(entry.getValue().getStartingQuantities().keySet());
        quantityTypes.addAll(entry.getValue().getEndingQuantities().keySet());
        stockOperations.addAll(entry.getValue().getOperationsApplied().keySet());
    }
    return stockItems;
}

As you see in the method, i fill both stockOperations and quantityTypes on every page load. These two correspond to my actual page columns. (These are atributes in my LazyDataModel implementation with their corresponding getters)

<p:dataTable id="movementsTable" var="item" 
                                 value="#{inventoryMovementsBean.view}" 
                                 rowIndexVar="rowIndex" 
                                 paginator="true"
                                 rows="25">
                        <p:column headerText="Item">
                            <h:outputText value="#{item.label}" />
                        </p:column>
                        <p:columns value="#{inventoryMovementsBean.view.quantityTypes}" var="quantityType" columnIndexVar="quantityTypeIndex">
                            <f:facet name="header">
                                #{quantityType.description}
                            </f:facet>
                            <h:outputText value="#{inventoryMovementsBean.view.getStartingQuantity(item, quantityType)} => #{inventoryMovementsBean.view.getEndingQuantity(item, quantityType)}" />
                        </p:columns>
                        <p:columns value="#{inventoryMovementsBean.view.stockOperations}" var="stockOperation" columnIndexVar="stockOperationIndex">
                            <f:facet name="header">
                                #{stockOperation.name}
                            </f:facet>
                            <h:outputText value="#{inventoryMovementsBean.view.getOperationValue(item, stockOperation)}" />
                        </p:columns>
                    </p:dataTable>

inventoryMovementsBean.view is my LazyDataModel instance and methods getOperationValue(item, stockOperation), getStartingQuantity(item, quantityType) and getEndingQuantity(item, quantityType) are the ones that give me the content given an item and a column.

Ive already made a debug, and im pretty sure the whole data is loaded propertly, it is just a display problem. Here are some screenshots as additional information.

初始渲染分页后的实际渲染分页后的预期渲染

These images correspond to:

  • Initial render (Page 1).
  • Actual render after pagination (Page 3)
  • Expected render after pagination (Page 3)

If you use Primefaces, There is an option like lazyLoading.If you specify true,then all the client side stuff will be handled in server side.For more

http://www.primefaces.org/showcase/ui/datatableLazy.jsf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM