Skip to content

docs: add Grid Pagination chapter and examples #4274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: latest
Choose a base branch
from

Conversation

bwajtr
Copy link
Contributor

@bwajtr bwajtr commented Apr 28, 2025

Added a chapter to the Grid documentation about the differences between Infinite Scrolling and Pagination.
Added pros and cons for both.
Also added Pagination implementation example in Flow, Lit and React. At the end the text is not that long, so I've decided to not add a new tab for this topic, but rather place it under Lazy Loading chapter

image

Copy link

github-actions bot commented Apr 28, 2025

AI Language Review

There are no issues that require improvement in the provided content.

private final TextField searchField = new TextField();

// tag::snippet[]
private final DataProvider<Person, Void> dataProvider = DataProvider.fromCallbacks(query -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use fromFilteringCallbacks instead

    private final CallbackDataProvider<Person, String> pagingDataProvider = DataProvider
            .fromFilteringCallbacks(query -> {


var offset = paginationControls.calculateOffset();
var limit = paginationControls.getPageSize();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

                // Update total item count here to avoid calling
                // dataSource.count twice
                paginationControls.update(itemCount);

grid.addColumn(Person::getProfession).setHeader("Profession");

grid.setAllRowsVisible(true); // this will prevent scrolling in the grid
grid.setDataProvider(dataProvider);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    var dataProvider = pagingDataProvider.withConfigurableFilter();
    grid.setDataProvider(dataProvider);

grid.setAllRowsVisible(true); // this will prevent scrolling in the grid
grid.setDataProvider(dataProvider);

paginationControls.update(dataSource.count(searchField.getValue()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paginationControls.update can be removed here when dataprovider did the update

searchField.setPlaceholder("Search");
searchField.setPrefixComponent(new Icon(VaadinIcon.SEARCH));
searchField.setValueChangeMode(ValueChangeMode.EAGER);
searchField.addValueChangeListener(e -> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove
paginationControls.update(dataSource.count(searchField.getValue()));
grid.getDataProvider().refreshAll();

And instead
// setFilter will refresh the data provider and trigger data
// provider fetch / count queries
dataProvider.setFilter(e.getValue());

Comment on lines +47 to +54
const pageCount = calculatePageCount(totalItemCount, pageSize.value);

useEffect(() => {
// Adjust the current page if it exceeds the new page count as a side effect of the total item count changing.
if (currentPage.value > pageCount) {
onCurrentPageChanged(pageCount);
}
}, [totalItemCount]);
Copy link
Member

@web-padawan web-padawan May 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider also changing totalItemCount to Signal<number>
Then you could use useComputed for pageCount e.g. like this:

const pageCount = useComputed<number>(() => {
  if (totalItemCount.value === 0) {
    return 1; // Display one page even if there are no items
  }
  return Math.ceil(totalItemCount.value / pageSize.value);
});

It might require some additional changes but it feels like signals should be used consistently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants