I want to implement pagination in my spfx webpart to show filtered list items. My list is having more than 5000 items, the filtered items may or may not be more than 5000. On each page i want to show 10 items only but in order to generate pagination i should know the total count of items according to my filter so that i can compute the number of pages. so i want to know is it possible to know the count of items for my filter criteria without actually getting the items because if we are getting count only after getting the actual items then what would be the purpose of get paginated items.
1 Answer
There is no way to determine how many items will be returned from a filtered query without executing the query first.
The good news however is, we integrated paging directly into PnPjs for you, so you don't have to do it. We use Async Iterators on all of our collection items, which means you can use async iterator methods to paginate through the data, and we'll handle the pagination for you. If you want 10 items at a time, use .top(10) if you want 100 items at a time, use .top(100)
const iterator = sp.web.lists.getByTitle("BigList").items.filter("Status eq 'Active'").top(10)[Symbol.asyncIterator]();
let results = await iterator.next();
console.log(results); // will output first 10 items
results = await iterator.next();
console.log(results); // will output next 10 items, and so on...
2 Comments
user2889674
This does make sense and i understood that from documentation that it does simplify things as we don't need to keep track of skiptokens but having said that this means we won't be able to show the page count, is this understanding is correct ?
Beau Cameron
That is correct. Unfortunately, SharePoint does not provide us the total page count when using the pagination via $skoptoken. There is no way to determine the total number of possible items without querying them all.