【问题标题】:Reset PrimeFaces DataTable state (filter, sorting, paging)重置 PrimeFaces DataTable 状态(过滤、排序、分页)
【发布时间】:2013-08-21 04:52:44
【问题描述】:

我想重置 PrimeFaces 数据表的筛选、排序和分页状态。不幸的是,没有简单的方法可以做到这一点。尤其是重置排序状态很困难。

到目前为止我所做的是:

DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent(componentId);
if (dataTable != null) {
    dataTable.setSortOrder("ascending");  // reset sortOrder
    dataTable.setFirst(0);                // reset page
    dataTable.setFilteredValue(null);     // reset filter
    dataTable.setFilters(null);
}

我正在使用 PrimeFaces 3.4.1。

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    最后,我找到了解决方案,隐藏在此示例 http://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml on tab ColumnsView.java:

    table.setValueExpression("sortBy", null);
    

    【讨论】:

    • 我可以用 Primefaces 5.3 dataTable.setValueExpression("sortBy", null) 和 dataTable.setSortBy(null);一起产生了一个在列标题中没有排序的数据表。
    • @alfonx 知道 PrimeFaces 7.0 上的情况如何?
    【解决方案2】:
    dataTable.setSortBy(null);
    

    我正在使用 PrimeFaces 4.0

    【讨论】:

    • 我可以用 Primefaces 5.3 dataTable.setValueExpression("sortBy", null) 和 dataTable.setSortBy(null);一起产生了一个在列标题中没有排序的数据表。 – alfonx 刚刚编辑
    【解决方案3】:

    在 LazyDataModel 加载中,我使用以下代码调用该方法:

    DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("form:dataTableItem");
    dataTable.setFirst(0);
    dataTable.reset();
    dataTable.setSortBy(null);
    

    【讨论】:

    • 这是一个很好的解决方案,也是更完整的解决方案,尤其适用于数据表未绑定到后端的情况。
    【解决方案4】:

    首先我像其他答案一样重置排序状态:

    dataTable.setSortBy(null);
    

    但是,这并没有为我重置表中显示的代码。它仍在显示先前过滤的结果,因为我已将数据表上的 filteredValue 属性设置为视图范围 bean 中的字段。我通过将我的 filteredValue 字段设置为 value 字段,将当前过滤结果设置为完整的数据集来解决了这个问题。

    【讨论】:

      【解决方案5】:
      private DataTable filtervalue;
      getter and setter of filtervalue ..........
      call resetValue(); method on datatable
      as  filtervalue.resetValue();
      

      希望有效果

      【讨论】:

      • 与问题中已经存在的问题(关于排序)相比,这有什么改进......请发布“真实”代码......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2011-09-22
      • 2011-10-18
      • 2018-01-13
      相关资源
      最近更新 更多