/home/muszek

would you really like fries with that?

CakePHP: How To Remember Pagination Sort Order

I have a few internal sites that help me manage data. CakePHP's paginate() function is wonderful when it comes to getting functional tables with content quickly. The thing about watching paginated data is that I use it in a certain context - for example right now I'm looking at all my recipes sorted by creation date, because I want to edit some of those that I added in the second half of last year. The problem is that whenever I click on something other than links generated by this paginator (for example I edit one of the recipes) and then go back to the list, I have to set sorting again. Here's a short code that makes Cake remember in session how were my recipes sorted last time:

if(isset($this->params['named']['sort'])) { // user clicked on a "sort by" link, write his choice to the session
  $this->Session->write('recipes_sort',array($this->params['named']['sort']=>$this->params['named']['direction']));
}
elseif($this->Session->check('recipes_sort')) { // user has "saved" his sorting preference before
  $this->paginate['order'] = $this->Session->read('recipes_sort');
}

Drop it in your controller's action (for example recipes/index) before you call $this->paginate();.

Hi, a different solution

Hi, a different solution could be storing $this->referer() in edit actions.

regards
dz

you cant use the referer as

you cant use the referer as if you go to /edit and save with $this->redirect($this->referer()) the referer is /edit. to use the referer you have to save it in a hidden field and then redirect to the value in there, which is just as much pain.

Have a look at Matt Curry's

Have a look at Matt Curry's method of doing this:
http://bakery.cakephp.org/articles/view/pagination-recall
I think it is much more general and does not require so many changes to the code.

Thanks Matt for wonderful

Thanks Matt for wonderful solution.

Code was easy to change to make it recall the show/limit parameters too.

Regards
Sid

 

My favorite bands

Pixies
Manu Chao
Bonobo
Massive Attack
Antonio Vivaldi
The White Stripes
Archive
T.Love
Noir Désir
Kult
 

My favorite albums

Doolittle by Pixies
Surfer Rosa / Come on Pilgrim by Pixies
White Blood Cells by The White Stripes
Classical Masterworks CD39 by Vivaldi, Antonio
Animal Magic by Bonobo
666.667 Club by Noir Désir
Prymityw by T.Love
Mezzanine by Massive Attack
Classical Masterworks CD 6 by Albinoni, Tomaso
Pressure Drop, The Best Of ... by Toots & The Maytals
 

My favorite tracks

Wave of Mutilation by Pixies
Hey by Pixies
Here Comes Your Man by Pixies
Debaser by Pixies
I Bleed by Pixies
Dead by Pixies
Intro by Bonobo
Tame by Pixies
Monkey Gone to Heaven by Pixies
Lazy by Noir Désir
 

Tabbos

Free and open source software
Ubuntu
Debian
Geany
Last.fm
Digg
Slashdot
Wikipedia
Drupal
CakePHP
KS Cracovia Kraków
Free Tibet
Pixies
Massive Attack
The Clash
Air
Röyksopp
Thievery Corporation
Bonobo
Manu Chao
5'nizza
 

Recent comments