javascript Sails.Js - 我如何在sails.Js 中进行分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26648059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sails.Js - How I do pagination in sails.Js
提问by sasitha999
I want to create paginated table using sails.js, mongodb and waterline-ORM.
我想使用sails.js、mongodb 和waterline-ORM 创建分页表。
Is there a any specific way to do pagination in sails.js?
在sails.js 中是否有任何特定的方法可以进行分页?
回答by Martin Malinda
http://sailsjs.org/#/documentation/concepts/ORM/Querylanguage.html
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();
如果您希望分页异步工作,使用 JQUERY$$.getJSON
和在服务器上很容易做到res.json();
Theres a lot of info in waterline and sails docs.
水线和风帆文档中有很多信息。
回答by fandyst
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
如果你想从前端获取数据,并且开启了blueprint,你也可以试试:http: //yourDomain.com/ModelName?skip=10&limit=10
Reference: 1.officer site: http://sailsjs.org/#/documentation/reference/blueprint-api/Find.html
参考: 1.officer 站点:http://sailsjs.org/#/documentation/reference/blueprint-api/Find.html
回答by xtrinch
You could build a functional paginator with built-in skip
& limit
query parameters for blueprint routes:
您可以为蓝图路由构建一个带有内置skip
和limit
查询参数的功能分页器:
/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.
使用此选项,您可以根据各种设备大小动态调整页面大小 - 您将提供此选项limit
,这基本上是您的页面大小。将(页面大小 - 1)乘以当前页码 - 瞧,您已经获得了skip
参数。
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:
至于如何获取所有item的数量,我还没有找到内置的方法,所以我写了一个小助手中间件(https://github.com/xtrinch/sails-pagination-middleware) 以这种方式返回响应 JSON 中的总数:
{
"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
.
您需要做的就是通过 npm 安装中间件并将其添加到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.
如果您需要一个功能齐全的示例,我在 github 上还有一个带有这种分页的示例待办事项应用程序:https: //github.com/xtrinch/vue-sails-todo。它是用 vue 编写的,但无论哪种情况,您都应该有所了解。
Note that this answer requires sails 1.x.
请注意,此答案需要sails 1.x。
回答by Noitidart
I think you can also do it with io:
我想你也可以用io来做到:
io.socket.get('/thing', {limit: 30, skip: 30*pageNum}, function(things, jwr) { /*...*/ })