23

I want to create paginated table using sails.js, mongodb and waterline-ORM.

Is there a any specific way to do pagination in sails.js?

Joe Hill
  • 333
  • 3
  • 12
sasitha999
  • 386
  • 1
  • 3
  • 12

4 Answers4

33

http://sailsjs.org/#/documentation/concepts/ORM/Querylanguage.html

Model.find().paginate({page: 2, limit: 10});

Model.find({ where: { name: 'foo' }, limit: 10, skip: 10 });

If you want the pagination to work asynchronously, its very easy to do with JQUERY $$.getJSON and on the server res.json();

Theres a lot of info in waterline and sails docs.

Martin Malinda
  • 1,573
  • 11
  • 20
  • 2
    I wrote some blueprints to help with pagination https://github.com/randallmeeker/SailsBluePrintActions/tree/master/pagination – Meeker Oct 30 '14 at 20:14
  • 2
    How do we get the total number in the same query though? Because i want to tell my view if it should show a "next button" or not, and to put the "page" crumbs (like 1, 2, 3, 4, 5) – Noitidart Aug 08 '19 at 15:43
  • @Meeker Nice job. Please also add this as an answer. It was super helpful to me. – Iman Mohamadi Oct 25 '21 at 09:59
10

There is also another way.

if you want to fetch data from the front-end, and have turned blueprint on, you can also try: http://yourDomain.com/ModelName?skip=10&limit=10

Reference: 1.officer site: http://sailsjs.org/#/documentation/reference/blueprint-api/Find.html

fandyst
  • 2,740
  • 2
  • 14
  • 15
  • 7
    which is awesome... but in order to generate a functional paginator in front en I need to get the initial values to my (angular) app... by initial I mean: total number of items and the items_per_page (if none specified it should be the default one in blueprint).... what is the proper pattern to get these? – DS_web_developer Nov 23 '15 at 12:25
2

You could build a functional paginator with built-in skip & limit query parameters for blueprint routes:

/api/todos?skip=10&limit=10

With this option, you could have dynamically sized page size according to various device sizes - this option you would provide with limit, which is basically your page size. Multiply (page size - 1) by current page number - voila you've got your skip parameter.

As to how to get the number of all items, I haven't found a built-in way to do it, so I've written a little helper middleware (https://github.com/xtrinch/sails-pagination-middleware) to return the total count in the response JSON this way:

{
    "results": [
        {
            /* result here */
        },
        {
            /* another result here */
        }
    ],
    "totalCount": 80
}

All you need to do is install the middleware via npm and add it to your middlewares in http.js.

If you need a fully functional example, I've also got an example to-do app with this sort of pagination on github: https://github.com/xtrinch/vue-sails-todo. It's written with vue, but you should get the idea either case.

Note that this answer requires sails 1.x.

xtrinch
  • 2,232
  • 1
  • 24
  • 46
  • This is excellent on the subject of returning total count. I'll take a look how you did that. Ideally I would like to get totalCount in the same query, without having to do another query to get total count. – Noitidart Aug 08 '19 at 15:44
0

I think you can also do it with io:

io.socket.get('/thing', {limit: 30, skip: 30*pageNum}, function(things, jwr) { /*...*/ })
Noitidart
  • 35,443
  • 37
  • 154
  • 323