Difference between #2 and #1 of Quick Tip about Pagination Params

unchanged
Title
Quick Tip about Pagination Params
unchanged
Category
Tips
unchanged
Tags
Pagination
changed
Content
Developing custom Grids and ListViews for my new CMS I was facing a small
challenge: If I was to update/delete/batch delete items of my Grid, how would I
return to the current page view? 

I know there a couple of extensions out there, but I do extensive use of AJAX
into my applications. Once a user logs in, the rest in my CMSs are pure AJAX, so
the extensions where of no HELP in this scenario.

If you check at the parameters of the pagers you see the parameter name to be on
the format of: Modelname_page.

In order to solve the issues (I wont expose the solution of the custom widgets
and components because they have to do with the CPagination object) of returning
back to the current page displayed I did this:

~~~
[php]
// in my globals.php -this file is imported on index.php

/**
 * Returns the named HTTP parameter.
 * This is the shortcut to Yii::app()->request->getParam($name)
 */
function getParam($name, $default=false){
	return Yii::app()->request->getParam($name, $default);
}

/**
 * Returns an array of matched Page parameters
 * based on the Model name given
 */
public function getPaginationParam( $modelName ){
    // format name appropiately *no misstyping errors allowed*
    $modelName = ucfirst(strtolower($modelName));
    // return the param
    return getParam($modelName.'_page') ?
array($modelName.'_page'=>getParam($modelName.'_page')):array();
}
~~~

Once I have that function, I can find out whether a pagination parameter was
submitted or not. Let's imagine a user clicks on the *delete* button and the
actionDelete of the *Bogus* controller is called, I would like after deletion to
return to the admin display of Bogus Models and to continue on the same page.

~~~
[php]
public function actionDelete(){
 if(Yii::app()->request->isPostRequest)
 {
   // we make use of getParam -see above
   // we should check whether the param 
   // returns false or not!!! This is too simple
   $this->loadModel( getParam('id') )->delete();
   //
   // lets get the page params of bogus if any
   $params = getPaginationParam('bogus');
   //
   // create the url appropiately
   $route = $this->createUrl('admin',$params);
   //
   // redirect to admin and finish
   $this->redirect($route,true);

 }catch(Exception $e){
   ....
~~~

Easy right? Now, what about update? Well, in my case, what I did, is to include
a hidden field into the form if there is a page parameter and on submission i do
the same on the actionUpdate as I did on the actionDelete of the
controller.











Write new article
  • Written by: Antonio Ramirez
  • Category: Tips
  • Yii Version: 1.1
  • Votes: +6
  • Viewed: 25,054 times
  • Created on: Dec 17, 2010
  • Last updated: Dec 17, 2010
  • Tags: Pagination