Making Table class compatible with pagination

In line 1273 of vaadin’s Table.java is this code:

int maxIndex = size - pageLength;

Is it possible to change this to:

int maxIndex = maxIndex(size, pageLength);

Where the overridable method maxIndex() would be defined:

protected int maxIndex(s, p) { return s - p; }

The reason for this is to allow a pagination implementation to keep the first index aligned with the pagination frame.

The original expression (maxIndex = size - pageLength) says that when displaying items in a table, the index of the first item being displayed (maxIndex) can be no higher than the total number of items in the table (size) minus the page length. This make sense. If you have 105 items, and your pageLength is 10, then the highest possible index for the first visible row of the table is 95 (= 105 - 10). The last items can be displayed in the table are from 95 to 104.

However, in a paginated table, then you want to align your output by page frame. In this case every frame has 10 items. If you have 105 items, then last frame would be from 100 to 110 and the last five items would be left blank, and not from 95 to 105.

By making the calculation of maxIndex a call to an overridable method, then it could be adjusted for pagination support.

I think line 984 would require the same treatment:

int maxIndex = size() - pageLength;

I have finished my testing and confirmed that the above change is the only change necessary to vaadin code to make pagination work.

My requirements were a paginated table that supported filtering. I first tried to use Jens Jansson’s PagedTable, but that did not directly support filtering.

I modified that code to use an IndexedContainer (which does support Filtering) instead of the PagedTableContainer.

Table and its base classes already seemed to have much of the support I needed to support pagination with filtering. The only change needed was to add a protected method to the Table class:
[b]

[indent]

protected int maxIndex(int size, int length) {
	return size - length;

[/indent]}

[/b]
and call it from two places in the same class:

line 984: int maxIndex = maxIndex(size(), pageLength);

line 1273: int maxIndex = maxIndex(size, pageLength);

I have attached three files which contain the new implementation:

  1. PagedTable.java – the modified implementation (notice that
    maxIndex()
    is overridden)
  2. PageChangeListener.java – separated out from PagedTable.java so it could be called from the filter
  3. PositionManagementFilters.java – uses SimpleStringFilter along with PagedTable

The paging behavior is the same as with the original PagedTable. However, with filtering, the total number of pages will change, depending on the results of the filter.

Also, there is a scroll bar along the side of the table. I think I could have made this go away with a filter which filtered out all rows except the ones visible on the current page. However, this filter would have to have been applied LAST. The current vaadin implementation of filters uses a HashSet, which doesn’t preserver order. So I don’t think it is possible without using a Set implementation that does preserve order.

On the other hand, I like having a functioning scroll bar along with the paging controls. Sometimes you just want to scroll up or down a little, in which case you can use your mouse’s scroll button. If you want to jump around between pages, you can use the page controls.

Using the pagination controls will move the scroll bar, but using the scroll bar will not change the “current page” field. I have not yet investigated what it would take to have movement of the scroll bar also update the “current page” field. Can anyone tell me if this is possible?

Anyway, my preliminary tests are working. Please have a look.

It would be great if these minor changes to the Table class could make it into 7.0.0.alpha2!

Thank you in advance.
12258.java (14.4 KB)
12259.java (400 Bytes)
12260.java (2.26 KB)