在Cakephp 3.3中将分页值添加到json响应 [英] Adding pagination value to json response in Cakephp 3.3

查看:73
本文介绍了在Cakephp 3.3中将分页值添加到json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将分页值的页数添加到Cakephp 3.3的json响应中.我遵循Cakephp 3.3文档使用paginator方法进行查询,并激活了json扩展名,您只需要在控制器的末尾添加.json并添加函数即可获得json响应.我得到的结果是分页的第一页,但是我想添加一个参数,以告诉我已经分页了多少页,以便可以显示可以继续进行多少页.我尝试获取的json响应示例如下,页面为分页器值:

I am trying to add the number of pages paginated value to the json response for Cakephp 3.3. I followed the Cakephp 3.3 documentation to make queries using the paginator method and activated the json extension where you just have to add .json to the end of your controller and the function to get a json response. The results I get are the first page of pagination, however I'd like to add a parameter before that tells me how many pages have been paginated so that I can show how many more pages can be continued to. Example of the json response i am trying to get is below with pages being the paginator value:

    { pages: [pages:5],
      clients: [
     {
       id: 1,
       first_name: "Kirby",
       last_name: "Bonner"
     }]
    }

已经多次告诉我要仔细阅读文档,但是老实说,我没有清楚地解释我要在此处进行的操作.如果有人可以将我引导至一个可以清楚地说明如何执行此操作的示例或文档,我将不胜感激.谢谢.

I've been told multiple times to look through the documentation, but to be honest I don't see a clear explanation of what I'm trying to do here. I'd really appreciate if someone could direct me to an example or documentation that explains clearly how to do this. Thank you.

推荐答案

我在以下位置找到了问题的答案:

I found the answer to the question at: http://florian-kraemer.net/2015/10/cakephp-angularjs-pagination/

但是您必须在完成查询后对分页进行验证,否则它将始终返回false;

However you have to put the validation for pagination after you complete the query otherwise it will always return false;

下面是代码示例:

    public function index() {

    $query = $this->Table->find();
    if (empty($this->request->params['paging'][$this->Table->alias()])) {
       $paging = false;
    } else {
       $paging = $this->request->params['paging'][$this->Table->alias()];
    }
    $this->set('records', $this->paginate($query));
    $this->set('paging', $paging);
    $this->set('_serialize', ['records', 'paging']);
    }

这篇关于在Cakephp 3.3中将分页值添加到json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆