简体   繁体   English

分页后如何刷新数据表动态列

[英]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. 我使用的是primefaces 3.1,并且有一个数据表来显示分页数据,其数据列是基于页面数据构建的,并通过<p:columns/>标记进行呈现。 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. 我已经尝试了<p:ajax event="page" update="myTable"/><p:ajax event="page" update="myTableContainer"/>碰碰运气。

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: 我已经实现了自己的LazyDataModel实现,并对其进行了扩展,使其覆盖了load方法的实现:

@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. 如您在方法中所见,我在每次页面加载时都同时填充了stockOperationsquantityTypes These two correspond to my actual page columns. 这两个对应于我的实际页面列。 (These are atributes in my LazyDataModel implementation with their corresponding getters) (这些是我的LazyDataModel实现中的属性以及它们对应的getter)

<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. ventoryMovementsBean.view是我的LazyDataModel实例,方法getOperationValue(item,stockOperation),getStartingQuantity(item,quantityType)和getEndingQuantity(item,quantityType)是为我提供给定项目和列的内容的方法。

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). 初始渲染(第1页)。
  • Actual render after pagination (Page 3) 分页后的实际渲染(第3页)
  • Expected render after pagination (Page 3) 分页后的预期渲染效果(第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 如果使用Primefaces,则有一个类似lazyLoading的选项。如果指定true,则所有客户端的内容都将在服务器端处理。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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