Quick Tip about Pagination Params

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

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:

// 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.

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 of the controller.

5 0
8 followers
Viewed: 31 026 times
Version: Unknown (update)
Category: Tips
Written by: Antonio Ramirez
Last updated by: Antonio Ramirez
Created on: Dec 17, 2010
Last updated: 7 years ago
Update Article

Revisions

View all history

Related Articles