11

While deleting rows from an iterating component like a <p:dataTable>, the current page needs to be reset to its immediate previous page, if all of the rows in the last page have been deleted. This is unfortunately not automated in <p:dataTable> with LazyDataModel<T>.

Linguistically, if a data table contains 11 pages with 10 rows per page and all of the rows on the 11th page i.e the last one are deleted, it should get the 10th page automatically (i.e. the immediate previous page) but this does not happen automatically (the current page remains stationary (11th) as if the data table itself gets emptied) unless explicitly coded somewhere in the associated backing bean.

Informally, the corresponding pseudo code segment would look something like the following.

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

Where first is the page offset (starting with 0), pageSize indicates rows per page and rowCount indicates the total number of rows from the associated data-store/database.

Practically :

@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.
}

The static utility method Utility#currentPage() is defined as follows.

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();
}

This is a piece of boilerplate code and should be avoided being repeated all over the place - in every managed bean using a <p:dataTable> with LazyDataModel<T>.

Is there a way to automate this process?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tiny
  • 27,221
  • 105
  • 339
  • 599
  • 2
    Best way is asking PF guys to fix paginator logic for that. – BalusC Aug 26 '15 at 06:31
  • 1
    And for the time being put it in a baseclass? – Kukeltje Aug 26 '15 at 07:17
  • The piece of code had already been repeated in every managed bean using a `` with `LazyDataModel` in the whole project. Therefore, moving it to a super class requires drastic changes in the current design - difficult to adopt. – Tiny Aug 26 '15 at 16:20
  • PF, DataTable is the best open source choice, but many features like pagination must be completly rewrited, in my case i rewrite the hole Server and Client side for professional performence – Nassim MOUALEK Aug 26 '15 at 16:21
  • 1
    @NassimMoualek: please substantiate. E.g. create a blogpost where you elaborate on all the details. PF datatable works nice for us with good professional performance – Kukeltje Aug 27 '15 at 03:56
  • 1
    @Tiny: then I think your are at a loss. Other then maybe overriding the PF classes. And no one can write a valid answer for **you** – Kukeltje Aug 27 '15 at 03:58
  • It is taken up to the issue tracker on [GitHub](https://github.com/primefaces/primefaces/issues/668). – Tiny Aug 31 '15 at 10:37
  • @Tiny, was the issue deleted for some reason? 404. – Vsevolod Golovanov Oct 31 '15 at 10:48
  • @VsevolodGolovanov : The issue is there with no comments and no other participants yet. You will need to login there to view the page. – Tiny Oct 31 '15 at 10:56
  • Logged in, still 404. Weird. – Vsevolod Golovanov Nov 03 '15 at 09:46
  • @VsevolodGolovanov : Issues are likely to be reported somewhere else then which I don't know. I don't know how to make them publicly available. – Tiny Nov 03 '15 at 10:35

0 Answers0