来自 DataProvider 的 Yii 分页变量 [英] Yii Pagination Variables from DataProvider

查看:26
本文介绍了来自 DataProvider 的 Yii 分页变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器操作中需要某些分页变量.

例如:

1.当前页码

2.当前页面偏移量

3.显示的总记录

即显示 2005 年的 31 到 40 条记录

我尝试了以下方法:

$dataProvider = NodesTerms::getNodesDataFromTerms($nodeId) ;$pagination = $dataProvider->getPagination();var_dump($pagination->getPageCount());//var_dump($pagination->currentPage);

我可以获得分页对象,但在 $pagination->currentPage$pagination->offset 等中为零 (0)....

我需要此信息来动态生成元页面 titledescription 与页面列表的操作有关,例如 pagetitle: page 3 of 10 for American Recipes...

感谢您对此的任何帮助.

解决方案

尝试设置 itemCount 在您的 dataProvider:

'pagination'=>array('pageSize'=>10,'itemCount'=>$count)

或者使用一个新的CPagination对象:

$pagination = new CPagination($count);$dataProvider = new CSqlDataProvider($sql, array(//... 其他配置'分页' =>$分页));

<小时>

这是如何工作的:

分页的 itemCount 在创建数据提供者期间设置,再次CSqlDataProviderfetchData 函数:

 $pagination->setItemCount($this->getTotalItemCount());

在创建 data-provider 时只使用传递给 pagination 属性的值,如果我们不传递 itemCount 值,则 的默认值>0 使用.
因此,如果我们想访问 offsetpageCountcurrentPageitemCount before调用 fetchData 我们必须显式设置 itemCount.

但是,如果我们在调用 fetchData 之后想要这些值,那么由于在 fetchData 中调用了 setItemCount,这些值已经正确填充.

一个清晰的例子(在数据提供者创建过程中没有传递itemCount):

$dataProvider = NodesTerms::getNodesDataFromTerms($nodeId);$pagination = $dataProvider->getPagination();var_dump($pagination->getPageCount());//这将是零$data=$dataProvider->getData();//getData 调用 fetchData()var_dump($pagination->getPageCount());//现在这将是正确的值

I need certain pagination variables in my controller action.

such as:

1.Current Page Number

2.Current Page offset

3.total records displayed

i.e. 31 to 40 of 2005 records displayed

I tried the following:

$dataProvider = NodesTerms::getNodesDataFromTerms($nodeId) ;     
  $pagination = $dataProvider->getPagination();
  var_dump($pagination->getPageCount());
  //var_dump($pagination->currentPage);

I can get the Pagination Object, but zero (0) in $pagination->currentPage or $pagination->offset etc....

I need this information to dynamically generate meta page title and description on actions with page listings such as pagetitle: page 3 of 10 for American Recipes...

Appreciate any help with this.

解决方案

Try setting the itemCount explicitly in your dataProvider:

'pagination'=>array(
    'pageSize'=>10,
    'itemCount'=>$count
)

Or use a new CPagination object:

$pagination = new CPagination($count);

$dataProvider = new CSqlDataProvider($sql, array(
    // ... other config
    'pagination' => $pagination
));


How this works:

The pagination's itemCount is set during creation of the data-provider and again in CSqlDataProvider's fetchData function:

    $pagination->setItemCount($this->getTotalItemCount());

During creation of data-provider only the values passed to the pagination property are used, and if we don't pass itemCount value, then the default of 0 is used.
So if we want to access offset or pageCount or currentPage or itemCount before the call to fetchData we have to set the itemCount explicitly.

However if we want those values after the call to fetchData then the values are already correctly populated due to the call to setItemCount within fetchData.

An example for clarity (without passing itemCount during data-provider creation):

$dataProvider = NodesTerms::getNodesDataFromTerms($nodeId);     
$pagination = $dataProvider->getPagination();
var_dump($pagination->getPageCount()); // this will be zero

$data=$dataProvider->getData(); // getData calls fetchData()

var_dump($pagination->getPageCount()); // now this will be correct value 

这篇关于来自 DataProvider 的 Yii 分页变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆