kolyasya:meteor-pagination

v1.0.0-beta.0Published 2 months ago

meteor-pagination

Package implements reactive pagination for Meteor apps. It is based on observeChanges callbacks which publish docs into client-side collection e.g.: posts.paginated.

Install

meteor add kolyasya:meteor-pagination

meteor npm install --save lodash.defaults lodash.pullall

Usage example

Check ./example directory for a full working example

Server

Full list of possible params is here — https://github.com/kolyasya/meteor-pagination/blob/main/example/packages/meteor-pagination/utils/defaultParams.js

Basic usage:

1publishPaginated({
2  collection: Posts, // Mongo collection
3  name: 'posts.paginated', // Publication name
4  customCollectionName: 'posts.paginated', // Client-side collection name
5  countsCollectionName: 'posts.paginated.count', // Client-side counts collection name
6});

Client

It is just an example. You need to adjust variables according to your app code.

1export default withTracker(({ perPage, page, sort }) => {
2  const totalRows = Counts.get('posts.paginated.count');
3
4  const paginatedPostsSub = Meteor.subscribe('posts.paginated', {
5    skip: page * perPage,
6    limit: perPage,
7    fields: {
8      title: 1,
9      content: 1,
10    },
11    sort,
12
13    cursorSelector: {},
14  });
15
16  return {
17    postsLoading: !paginatedPostsSub.ready(),
18    posts: PostsPaginated.find().fetch(),
19    totalRows,
20  };
21})(Table);