cakephp 分页助手在从最后一页删除最后一条记录时显示错误

2024-01-29

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

<?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 开始 -超出范围的页面请求 http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html#out-of-range-page-requests会抛出异常。

然而,文档说分页参数将在 $this->request->params['paging'] 中可用是不正确的,因为这些参数是定义的after抛出异常。 (这个问题在两个月前已在 CakePHP 2.4.x 中修复)

因此,为了获得您想要的效果,您可以在控制器中执行以下操作:

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
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cakephp 分页助手在从最后一页删除最后一条记录时显示错误 的相关文章

随机推荐