11

从像 a 这样的迭代组件中删除行时,如果最后一页中的所有行都已删除<p:dataTable>,则当前页面需要重置为其上一页。不幸的是,这不是自动化的。<p:dataTable>LazyDataModel<T>

从语言上讲,如果一个数据表包含 11 页,每页 10 行,并且第 11 页上的所有行(即最后一页)都被删除,它应该自动获取第 10 页(即前一页),但这不会自动发生(当前页面保持静止(第 11 页),就好像数据表本身被清空一样)除非在关联的支持 bean 中的某处显式编码。

非正式地,相应的伪代码段如下所示。

if (rowCount <= (ceiling)((first + 1) / pageSize) * pageSize - pageSize) {
    first -= pageSize;
}

页偏移量在哪里first(以 开头0),pageSize表示每页的行数,并rowCount表示来自关联数据存储/数据库的总行数。

实际上:

@Override
public List<Entity> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {

    // ...

    int rowCount = service.getRowCount();
    setRowCount(rowCount);

    // ...

    if (pageSize <= 0) {
        // Add an appropriate FacesMessage.
        return new ArrayList<Entity>();
    } else if (first >= pageSize && rowCount <= Utility.currentPage(first, pageSize) * pageSize - pageSize) {
        first -= pageSize;
    } else if (...) {
        // ...
    }

    // ...

    return service.getList(first, pageSize, map, filters);
    // SortMeta in List<SortMeta> is PrimeFaces specific.
    // Thus, in order to avoid the PrimeFaces dependency on the service layer,
    // List<SortMeta> has been turned into a LinkedHashMap<String, String>()
    // - the second last parameter (named "map") of the above method call.
}

静态实用程序方法Utility#currentPage()定义如下。

public static int currentPage(int first, int pageSize) {
    return first <= 0 || pageSize <= 0 ? 1 : new BigDecimal(first + 1).divide(new BigDecimal(pageSize), 0, BigDecimal.ROUND_CEILING).intValue();
}

这是一段样板代码,应该避免在所有地方重复 - 在每个使用<p:dataTable>with的托管 bean 中LazyDataModel<T>

有没有办法自动化这个过程?

4

0 回答 0