UI Toolbox logoUI Toolbox

Pagination

A Pagination control has the page buttons for a list that shows one page at a time: previous, the page numbers and next. It always shows the first page, the last page and the pages next to the current one, with "…" for the pages between.

Page buttons: previous, 1, an ellipsis, 5, 6 (the current page), 7, an ellipsis, 12, next

How to page a list:

  1. Add a Pagination under the list.
  2. Give it the number of items and the items on one page.
  3. In PageChanged, show the items of the page.
<tb:Pagination name="pages" />
const int pageSize = 10;
var pages = root.Q<Pagination>("pages");
pages.SetItemCount(saves.Count, pageSize);
pages.PageChanged += showPage;
showPage(pages.Page);

void showPage(int page)
{
    list.itemsSource = saves.Skip((page - 1) * pageSize).Take(pageSize).ToList();
    list.Rebuild();
}
Member Description
PageCount (page-count) The number of pages (at least 1). When the count goes below the current page, Page moves to the last page.
Page (page) The current page, from 1. A value outside the range is clamped.
SiblingCount (sibling-count) The pages to show on each side of the current page (default 1).
SetItemCount(itemCount, pageSize) Sets PageCount from a number of items, rounded up to full pages.
PageChanged Raised after Page changed, with the new page. Not raised when the value stays the same.
ShowLabels (show-labels) Shows "Previous" and "Next" next to the arrows. Off by default.
ShowFirstLast (show-first-last) Shows a "«" button for the first page and a "»" button for the last page. Off by default.
PageSizes (page-sizes) The page sizes the user can pick, such as 10,25,50. Empty (the default) shows no selector.
PageSize (page-size) The items on one page (default 10). The selector and SetItemCount set it.
PageSizeChanged Raised after PageSize changed, with the new size.
Pagination.PagesToShow(page, pageCount, siblingCount) The page numbers that show, in order, with Pagination.ELLIPSIS (0) for a gap. Use it to make your own page buttons.

A pager with all the parts:

<tb:Pagination name="pages" show-labels="true" show-first-last="true" page-sizes="10,25,50" />
pages.SetItemCount(saves.Count, 10);
pages.PageChanged += showPage;
// The page count is new already; show the items of the page again
pages.PageSizeChanged += _ => showPage(pages.Page);

Tips:

Styling

Class Element
tb-pagination The row: previous, the pages, next.
tb-pagination__button Each button: 24 px high, at least 24 px wide, a border and no background. :hover uses --tb-color-surface-hover; :focus has the accent border.
tb-pagination__button--current The button of the current page, in the accent color.
tb-pagination__previous, tb-pagination__next The "‹" and "›" buttons.
tb-pagination__first, tb-pagination__last The "«" and "»" buttons.
tb-pagination__sizes The page size selector: a label (tb-pagination__sizes-label) and a DropdownField.
tb-pagination__pages The row of page buttons and "…" labels.
tb-pagination__ellipsis A "…" label, 20 px wide at least, in --tb-color-text-muted.

To center the control and make the buttons round:

.tb-pagination { justify-content: center; }
.tb-pagination .tb-pagination__button { border-radius: 12px; }