简体   繁体   English

使用angularJS实现后端分页的最佳方法是什么?

[英]What is the best way to implement backend pagination using angularJS?

I have a situation where I want to implement back end pagination but, I am not sure about how to do it. 我有一种情况,我想实现后端分页,但我不知道如何做到这一点。 I am thinking of a solution where I fetch the total pages (a page is a list of n defined element ) and then make call to the server to fetch 1st, 2nd, 3rd, 4th , .... n-th page and so on. 我正在考虑一个解决方案,我获取总页数(一个页面是n定义元素的列表),然后调用服务器来获取第1页,第2页,第3页,第4页......第n页等等上。 Any suggestions ? 有什么建议么 ?

Generally, pagination is done by specifying to the user how many results to display per page, and which page of results he is currently viewing. 通常,通过向用户指定每页显示多少结果以及他当前正在查看哪个结果页来完成分页。 However, backend storage such as MySQL or Solr typically handle pagination by specifying how many results to display per page, and how many results to skip in the output. 但是,MySQL或Solr等后端存储通常通过指定每页显示的结果数以及输出中要跳过的结果数来处理分页。 I generally use something like the following PHP to get the later (for the backend) out of the former (from the user): 我通常使用类似下面的PHP来获取前者(来自用户)的后者(后端):

$documentsPerPage = 10;

if ( isset($input['page']) && is_numeric($input['page']) && $input['page']>0 ) { 
    $skip = $documentsPerPage * (intval($input['page'])-1);
} else {
    $skip =0;
}

$sql = "SELECT * FROM someTable LIMIT {$skip}, {$documentsPerPage}";

If you have $count records in the database, here is how to calculate how many pages you have with a simulated floor-up operation: 如果数据库中有$count记录,下面是如何计算模拟楼层操作的页数:

$totalPages = ($count + $documentsPerPage - 1) % $documentsPerPage;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM