Hi
I just worked on improving this for myself and was looking around for other people wondering about the same. There are some on other pages in this forum too (example: Other discussion ).
I’ll post my solution here as the post is more recent. [Edit: Unfortunately, I had to review this solution and in the end it is not as advantageous as expected - I end up rendering the grid internally (to make sure that the pager id is generated) - an improvement may be to create a dummy dataprovider when the grid does not need to be rendered].
I must say I got a bit disappointed as I can not easily extend core classes (e.g., by telling the factory to look for another class in place of the default one).
Here is how I went about it:
// Creation of a replacement class for the grid view.
// The ‘ajax’ variable already in place is used. That variable seems unused in the framework itself - I wonder why. Somebody must have had a good intention and
// then gave up on it.
// This file is placed in the ‘components’ directory.
<?php
Yii::import('zii.widgets.grid.CGridView');
class MyCGridView extends CGridView {
public $isRender = false; // If the configuration sets 'isRender' to true, then rendering is 'forced'.
public function init() {
/* @var $request CHttpRequest */
$request = Yii::app()->request;
$ajax = $request->getParam($this->ajaxVar,null);
if($ajax!==null) {
$this->isRender |= array_search($this->getId(),explode(',',$ajax))!==false;
} else {
$this->isRender = true;
}
return parent::init();
}
public function run() {
if($this->isRender) {
return parent::run();
} else {
// Unfortunately we have to render the grid to make sure that he
// 'id' numbering works.
ob_start();
ob_implicit_flush(false);
parent::run();
ob_get_clean(); return null; return null;
}
}
}
Then this replacement has to get used somehow, so I setup the CGridView widget like this:
$grid=$this->widget(CGRIDVIEW, array(/*Usual configuration */));
// ...
CGRIDVIEW in the above is a constant as I’ld like to switch easily between one implementation or the other.
The constant is defined in my ‘index.php’:
defined('CGRIDVIEW') or define('CGRIDVIEW','MyCGridView');
defined('CGRIDVIEW') or define('CGRIDVIEW','zii.widgets.grid.CGridView');
The first line actually defines the constant, the second line is there to allow me to comment out the first line and have the ‘default’ gridview behavior.
It would be nice if some next version of Yii allows changing class references more easily. For instance by stating somehow in the configuration that ‘zii.widgets.grid.CGridView’ should correspond to ‘MyCGridView’.