NPager add a dropdownlist for the CListView and the CGridView. Whit this dropdownlist user can set up the pagination pageSize value.
The idea comes from a friend. Her name begins with N, so NPager the extension name.
Requirements ¶
I created with Yii 1.1.10, but perhaps it working with older versions too.
Usage ¶
/* NListView */
//the action:
public function actionNListView()
{
$dataProvider = new CArrayDataProvider($this->getArray(), array(
'sort' => array(
'attributes' => array(
'id', 'settlement', 'type', 'population'
),
),
'pagination' => array(
'pageSize' => isset($_GET['pageSize']) ? $_GET['pageSize'] : 12,
// 12 is the default pageSize
),
));
$this->render('NListView', array(
'dataProvider' => $dataProvider,
));
}
/*the view's content:
defaults:
pagerlist => array(
'10' => 10,
'25' => 25,
'50' => 50,
'100' => 100,
'all' => 'ALL', // you can use the zii dictionary
),
pagerlistCssClass => 'pager-list',
template => "{summary}\n{pagerlist}\n{pager}\n{items}",
textItemsPerPage => 'items per pages', // you can use the zii dictionary
*/
$this->widget('application.extensions.NPager.NListView', array(
'dataProvider' => $dataProvider,
'sortableAttributes' => array(
'id', 'settlement', 'type', 'county', 'population'
),
'itemView' => '_item',
'pagerlist' => array(
'4' => '4',
'12' => '12',
'20' => '20',
'40' => '40',
'80' => '80',
'160' => '160',
'all' => 'All',
),
));
?>
/*********************************************************************/
/*NGridView*/
//the action:
public function actionNGridView()
{
$dataProvider = new CArrayDataProvider($this->getArray(), array(
'sort' => array(
'attributes' => array(
'id', 'settlement', 'type', 'population'
),
),
'pagination' => array(
'pageSize' => isset($_GET['pageSize']) ? $_GET['pageSize'] : 10,
// 10 is the default pageSize
),
));
$this->render('NGridView', array(
'dataProvider' => $dataProvider,
));
}
/*the view's content:
defaults:
pagerlist => array(
'10' => 10,
'25' => 25,
'50' => 50,
'100' => 100,
'all' => 'All', // you can use the zii dictionary
),
pagerlistCssClass => 'pager-list',
template => "{summary}\n{pagerlist}\n{pager}\n{items}",
textItemsPerPage => 'items per page', // you can use the zii dictionary
*/
$this->widget('application.extensions.NPager.NGridView', array(
'dataProvider' => $dataProvider,
));
Suggestion on generalizability
This is nice extension, thanks tocsa.
my testdrive need some improve on both classes such as
In Yii::import('zii.widgets.grid.CGridView'); Yii::import('ext.NPager.NPager'); remove Yii::import('zii.widgets.grid.CGridView'); and in $basePath = Yii::getPathOfAlias('ext.NPager.assets'); $baseUrl = Yii::app()->getAssetManager()->publish($basePath); to $basePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR; $baseUrl = Yii::app()->getAssetManager()->publish($basePath, false, 1, YII_DEBUG); and in $url = $controller->createUrl($route, $parameters); /** * Since version 1.0.3, if the controller belongs to a module, the {@link CWebModule::getId module ID} * will be prefixed to the route. (If you do not want the module ID prefix, the route should start with a slash '/'.) */ so in $route = $controller->route; will call $this->getUniqueId() and finally $url = $controller->createUrl($route, $parameters); call again if($route[0]!=='/' && ($module=$this->getModule())!==null) $route=$module->getId().'/'.$route; therefore, $route = $controller->route; to $route = "/".$controller->route; in url = {url:url+$('#'+id+'>.$this->pagerlistCssClass>select').val()}; it seem not catch all UrlManager case, at last in my test drive need alter to url = {url:url+"/"+$('#'+id+'>.$this->pagerlistCssClass>select').val()}; like http://www.yiiframework.com/extension/npager/page/1/pageSize/10 finally if template => "{summary}\n{pagerlist}\n{pager}\n{items}", as you suggest but change as following template => "{summary}\n{pagerlist}\n{pager}\n{items}\n{pagerlist}\n{pager}", seem not work below pagerlist if number of page less than minimum page, better like pager as hide it look forward yr new upgrade
best wish
Updating the Cgridview/Clistview upon selection
I just check the demo , when i select the number from dropdown its NOT updating the view!
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.