Important Notice - Forums is archived
To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.
Table Paging , it it OK ?
I feel sick about infinite scrolling table , I don't know why vaadin so obessed with infinite scroll table.
So I build my own.
public class PagedTable<T> extends CustomComponent {
private final MTable table;
/** 1-based */
private int currentPage = 1;
private int PAGESIZE = 10;
private Logger logger = LoggerFactory.getLogger(getClass());
private final Function<Pageable , List<T>> listFunction;
private String sortProperty = "id";
private boolean sortAsc = false;
public PagedTable(MTable table, Supplier<Long> countSupplier, Function<Pageable, List<T>> listFunction) {
this.listFunction = listFunction;
long total = countSupplier.get();
long pages = total % PAGESIZE == 0 ? total / PAGESIZE : total / PAGESIZE + 1;
logger.debug("count = {} , pages = {}", countSupplier.get(), pages);
VerticalLayout layout = new MVerticalLayout();
layout.setSizeFull();
this.table = table;
this.table.addSortListener(event -> {
this.sortProperty = event.getSortProperty();
this.sortAsc = event.isSortAscending();
refresh();
});
layout.addComponent(table);
MHorizontalLayout buttons = new MHorizontalLayout().withFullWidth();
IntStream.range(1, (int) pages + 1).forEach(page -> buttons.addComponent(new MButton("Page " + page, clickEvent -> {
currentPage = page;
logger.debug("set currentPage to {}", page);
refresh();
})));
layout.addComponent(buttons);
setCompositionRoot(layout);
refresh();
}
public void refresh() {
table.removeAllItems();
OnePageRequest request = OnePageRequest.of(currentPage , PAGESIZE , sortProperty , sortAsc);
List<T> list = listFunction.apply(request);
logger.info("{}" , request);
logger.debug("loading currentPage = {} , list size = {}", currentPage, list.size());
table.addBeans(list);
table.setPageLength(list.size());
}
}
And in the client side :
this.pTable = new PagedTable<>(mTable, userDao::count, onePageRequest -> {
logger.debug("onePageRequest = {}" , onePageRequest);
String sortProperty = "id";
boolean sortAsc = false;
Sort sort = onePageRequest.getSort();
if (sort != null) {
Sort.Order order = sort.iterator().next();
sortProperty = order.getProperty();
sortAsc = order.isAscending();
}
return userDao.list(onePageRequest.getPageNumber() , onePageRequest.getPageSize() , sortProperty , sortAsc);
});
addComponent(pTable);
The Pageable and Sort are from Spring.
The OnePageRequest is just another subclass of AbstractPageRequest , with 1-based index.
The result (see the attachments)
Of course , there are some things need to be improved , such as buttons , which should print first 3 and last 3 buttons is OK.
And I see some Container or BeanItem class in vaadin API , but I am not sure whether I need them.
But it seems OK now.
I am just new to Vaadin about one week , this is my preliminary work . Any suggestions are welcome.
I read the demo code , it seems loading all items , not what I want.
Is there any example that PagedTable ( https://vaadin.com/directory#!addon/pagedtable ) making use of DAO , retrieving Items page by page ?