当从最后一页删除最后一条记录时,cakephp paginator助手会显示错误 [英] cakephp paginator helper shows error when delete the last record from the last page

查看:103
本文介绍了当从最后一页删除最后一条记录时,cakephp paginator助手会显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果从最后一页删除最后一条记录,我只想将其重定向到最后一个索引。
请帮助我做到这一点。

I just want to redirect that to the last index if the last record is deleted from the last page. please help me to do this.

<?php                       
    echo $this->Paginator->prev
      ($this->Html->image('prev.png'), array('escape' => false), 
    array(), null, array('class' => 'prev'));
    echo $this->Paginator->counter
      ('Page {:page} of {:pages}, Total Records {:count}');                     
    echo $this->Paginator->next($this->Html->image
      ('next.png'), array('escape' => false), 
        array(), null, array('class' => 'next'));
 ?>


推荐答案

从CakePHP 2.3开始-超出范围的页面请求将引发

As of CakePHP 2.3 - Out of Range page requests will throw an exception.

,但是文档不正确,因为在$ this-> request-> params ['paging']中可以使用分页参数,因为这些是在之后定义的,所以会引发异常。 (这个问题已经在两个月前的CakePHP 2.4.x中得到了解决

However the documentation is not correct in saying that the paging parameters will be available to you in $this->request->params['paging'], because those are defined after the exception is thrown. (This problem has been fixed in CakePHP 2.4.x two months ago)

因此,要获得想要的效果,您可以执行以下操作

So, to get the effect you want you can do something like this in your controller:

public function index() {
    try {
        $paginatedData = $this->Paginator->paginate();
    } catch (NotFoundException $e) {
        //get current page
        $page = $this->request->params['named']['page'];
        if( $page > 1 ){
            //redirect to previous page
            $this->redirect( array( "page" => $page-1 ) );
        }else{
            $paginatedData = array(); //no data to paginate so use empty array()
                                      //you will have to check for this in the view and no longer display the pagination links, since they will NOT be defined
        }
    }
}

这篇关于当从最后一页删除最后一条记录时,cakephp paginator助手会显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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