只有当您可以以某种方式将存储过程应用于Limit
(最大记录检索) 和Offset
(从哪个记录开始)时,才能完成此操作。
让我们通过例子一步步走,并分析所需要的
你的Yii控制器,图像
function actionIndex(){
//Not needed, you apply criteria in your stored procedure with the
//arguments of your stored procedure.
//$criteria=new CDbCriteria();
// In some way you have to be able to determine how many records will
// be returned by the stored procedure when no limits are set
// (your challenge).
$count= ..; // challenge here
// create pagination object
$pages=new CPagination($count);
// Results per page, configure the maximum number of rows to be
// displayed (the limit).
$pages->pageSize=10;
// Set the page you are on. Either the starting page (0) or the page
// when the action is called by the linker (clicking on a button to
// go to a certanin page). The linker will send the page using get.
// Pages are zero based so you have to decrease the received page by
// 1.
$pages->currentPage = (isset($_GET['page']) ? $_GET['page']-1 : 0)
// You can not use this. This method applies the criteria to a plain
// vanilla query (not a stored procedure) and extends that query with
// a limit and offset.
//$pages->applyLimit($criteria);
// Instead, you retrieve the limit and offset from the pagination
// object and pass it to your stored procdure. F.i.
$command->bindParam(":limit",$pages->limit,PDO::PARAM_INT);
$command->bindParam(":offset",$pages->offset,PDO::PARAM_INT);
// Get the the images with the stored procedure.
$images=$command->queryAll();
// Display the images using a view
$this->render('index', array(
'images' => $images,
'pages' => $pages
));
}
所以,这是你必须做什么,如果你想使用上述方法:
- 看看你的存储过程支持
Limit
和Offset
- 如果是这样,扩展存储pocedure的参数来接收
Limit
和Offset
,并将其应用
- 找到一种方法来确定存储过程会导致当未设置
Limit
和Offset
中的记录数。这是必要的,因此分页对象可以基于当前页希望这将让你在赛道上
计算Offset
...
Thanks.I是能够通过使用CSqlDataProvider.Now的做分页问题是我正在使用renderPartial()在另一个视图中使用ajax显示此视图的内容。但是,当我点击分页链接时,它将采取行动。 Ajax无法正常工作。 – user1690835 2014-10-27 14:29:13