Pagination Recall

by mattc
Does it drive you nuts when you edit an item on page 4 of a list, then when you save you're dropped back to page 1 with the list back in the default order? The PaginationRecall component automatically remembers the page you were on and the sorting so you're returned to the same spot on the list.
I’ve been using the built in pagination in CakePHP 1.2-beta a lot lately and I must say it’s freaking awesome. My one oh so minor grievance is that it doesn’t "remember" where you were if you navigate off the page. For example many times I'll be paging through a sorted list of records and want to edit a particular one. After I edit and save I'm dropped back to the index with the default sorting on page 1.

Enter the PaginationRecall component. It will automatically retain your pagination settings so that you can edit/delete/surf around and return to the same spot on list.

Just include the component in your controller:
var $components = array('PaginationRecall');

Component Class:

<?php 
/*
 * Pagination Recall CakePHP Component
 * Copyright (c) 2008 Matt Curry
 * www.PseudoCoder.com
 *
 * @author      mattc <matt@pseudocoder.com>
 * @version     1.0
 * @license     MIT
 *
 */

class PaginationRecallComponent extends Object {
  var 
$components = array('Session');
  var 
$Controller null;

  function 
startup(&$controller) {
    
$this->Controller = & $controller;

    
$options array_merge($this->Controller->params,
                           
$this->Controller->params['url'],
                           
$this->Controller->passedArgs
                          
);

    
$vars = array('page''sort''direction');
    
$keys array_keys($options);
    
$count count($keys);
    
    for (
$i 0$i $count$i++) {
      if (!
in_array($keys[$i], $vars)) {
        unset(
$options[$keys[$i]]);
      }
    }
    
    
//save the options into the session
    
if ($options) {
      if (
$this->Session->check("Pagination.{$this->Controller->modelClass}.options")) {
        
$options array_merge($this->Session->read("Pagination.{$this->Controller->modelClass}.options"), $options);
      }
      
      
$this->Session->write("Pagination.{$this->Controller->modelClass}.options"$options);
    }

    
//recall previous options
    
if ($this->Session->check("Pagination.{$this->Controller->modelClass}.options")) {
      
$options $this->Session->read("Pagination.{$this->Controller->modelClass}.options");
      
$this->Controller->passedArgs array_merge($this->Controller->passedArgs$options);
    }
  }
}
?>

Report

More on Components

Advertising

Comments

  • behnish posted on 02/17/11 10:20:08 AM
    hey that was really helpful
  • CakeNoob posted on 05/03/09 11:50:33 AM
    To achieve the same thing without using the session, create your action link and pass the current page as a parameter like so:

    $paginator->current()

    Then in your controller, when you have completed what you need to, do this:

    $this->redirect($this->referer()."/page:".$page);
    where $page is the extra parameter you passed in above.
  • tarapage posted on 02/19/09 03:20:10 PM
    Thanks so much for this component, I've been trying to get AJAX Pagination to work exactly as you described above. I cannot use the REFERER in the case of AJAX, because the URL for the referer is the base URL, not the URL with all of the sort parameters and page information in it... This worked as soon as I downloaded it. I wish I found it yesterday, I could have saved myself a HUGE headache :)

  • silverknightutah posted on 12/15/08 08:07:50 AM
    I was using $referer until just recently a friend of mine pointed out that my code was not acting at all as expected on his machine. I found that his browser was passing the root of the site as the referrer and because of that, I'm quite happy to find this component which does not suffer this issue because of it's use of sessions rather than referrer, and I thank the author for writing it.

    (After some research, I found that my friend was using a Firefox extension that purposely mangles his referrer data and does several other annoying things in the name of "security", but it does make one wonder how many other folk are using similar tools and blame their troubles with websites on the web designer rather than on their own choice of "security" tools. Something to think about...)
  • mattc posted on 11/03/08 08:56:54 PM
    Not sure where the rest of the article went. If you're looking for the code it's available at http://github.com/mcurry/cakephp/tree/master/components/pagination_recall
  • z_sw posted on 03/27/08 01:58:57 AM
    Notice (8): Undefined index: url
  • mattc posted on 02/16/08 05:37:52 PM
    ...the problem could easily be avoided if instead of redirecting to the index, you redirected to (the referer url when they first entered the edit function) when the edit was finished.
    Doh! I knew as soon as I published this someone would show me an easier way. Thanks for the tip Andy.
  • boriscy posted on 02/11/08 04:14:37 PM
    I agree with Andy I have used that technique because it does not only redirects to the index it can redirect you from a diferent controller, anyway I think the component is nice.
  • wokowy posted on 02/05/08 02:19:36 AM
    Your component is very useful. Many times when I modified something I was dropped to the first page. Now it's more comfortable :]
    • AD7six posted on 02/05/08 08:40:27 AM
      For example many times I'll be paging through a sorted list of records and want to edit a particular one. After I edit and save I'm dropped back to the index with the default sorting on page 1. Hi, from the above description the problem could easily be avoided if instead of redirecting to the index, you redirected to (the referer url when they first entered the edit function) when the edit was finished.
      The only time what i propose wouldn't work is if you are storing settings in the session.
login to post a comment.