@@ -38,6 +38,7 @@ public class Query<T, F> implements Serializable {
38
38
private final List <QuerySortOrder > sortOrders ;
39
39
private final Comparator <T > inMemorySorting ;
40
40
private final F filter ;
41
+ private Integer pageSize ;
41
42
42
43
/**
43
44
* Constructs a Query for all rows from 0 to {@link Integer#MAX_VALUE}
@@ -119,24 +120,45 @@ public int getLimit() {
119
120
* <p>
120
121
* Vaadin asks data from the backend in paged manner. This shorthand
121
122
* calculates the page index for backends using paged data access, such as
122
- * Spring Data repositores .
123
+ * Spring Data repositories .
123
124
*
124
125
* @return the zero-based page index
125
126
*/
126
127
public int getPage () {
127
- return getOffset () / getLimit ();
128
+ int pageSize = getPageSize ();
129
+ int pageOffset = getOffset ();
130
+ // If page offset is not evenly divisible with pageSize raise
131
+ // pageSize until it is.
132
+ // Else we will on the end pick the wrong items due to rounding error.
133
+ if (pageOffset > pageSize && pageOffset % pageSize != 0 ) {
134
+ while (pageOffset % pageSize != 0 ) {
135
+ pageSize ++;
136
+ }
137
+ setPageSize (pageSize );
138
+ }
139
+ return pageOffset / pageSize ;
140
+ }
141
+
142
+ private void setPageSize (Integer pageSize ) {
143
+ this .pageSize = pageSize ;
128
144
}
129
145
130
146
/**
131
147
* Returns the page size that should be returned. The amount of items can be
132
148
* smaller if there is no more items available in the backend.
133
149
* <p>
134
- * Vaadin asks data from the backend in paged manner. This is an alias for
135
- * {@link #getLimit()}.
150
+ * Vaadin asks data from the backend in paged manner.
151
+ * <p>
152
+ * This is an alias for {@link #getLimit()} if the page offset can be evenly
153
+ * divided by the limit. Else the page size will be increased to evenly
154
+ * divide offset so the items skip for page will go to the correct item.
136
155
*
137
156
* @return the page size used for data access
138
157
*/
139
158
public int getPageSize () {
159
+ if (pageSize != null ) {
160
+ return pageSize ;
161
+ }
140
162
return getLimit ();
141
163
}
142
164
0 commit comments