Here I summarize some common yii pagination and instance code in Yii, there are common pagination and AJAX implementation pagination, I hope this article will be helpful to everyone.
First: CListView pagination of data for object form
Controller:
Copy Code code as follows:
Public Function Actionajax () {
$criteria = new Cdbcriteria ();
$criteria->order = ' news_id DESC ';
$criteria->condition = ' user_id = 1 ';
$dataProvider = new Cactivedataprovider (' News ', Array (
' Pagination ' => array (
' PageSize ' => yii::app ()->params[' pageSize '],
' Pagevar ' => yii::app ()->params[' Pagevar '],
),
' Criteria ' => $criteria,
));
$this->render (' View ', Array (
' Dataprovider ' => $dataProvider,
));
}
View:
Copy Code code as follows:
<?php
$this->widget (' Zii.widgets.CListView ', Array (
' Dataprovider ' => $dataProvider,//Data
' Itemview ' => ' _view ',//display template
' ID ' => yii::app ()->controller->id,
' Itemstagname ' => ' ul ',
' Ajaxvar ' => ',//default to page or Ajax after removing URL more concise
' Htmloptions ' => Array (' class ' => Yii::app ()->controller->id),
' Loadingcssclass ' => ' loading ',//default to List-view-loading
' Template ' => ' {summary}{sorter}{items}{pager} ',//display order
' Ajaxupdate ' => false,//whether the Ajax page false or the container ID of the paging display
' Beforeajaxupdate ' => ' before_ajax_update ',//callback function completed in Common.js
' Afterajaxupdate ' => ' after_ajax_update ',
' Emptytext ' => '
<div class= "Alert alert-waning" >
No data!
</DIV>
',//No data display content
' Pagercssclass ' => ' pagination ',//pagination class
' Pager ' => Array (
' Selectedpagecssclass ' => ' active ',//class of current page
' Hiddenpagecssclass ' => ' disabled ',//Disable page class
' Header ' => ',//content displayed before pagination
' Maxbuttoncount ' => 10,//Display paging quantity
' Htmloptions ' => Array (' class ' => '),
' Firstpagelabel ' => ' home ',
' Nextpagelabel ' => ' next page ',
' Prevpagelabel ' => ' on a page ',
' Lastpagelabel ' => ' last ',
),
));
?>
Second: Clinkpager data paging for array form
Controller:
Copy Code code as follows:
Public Function Actionindex () {
$criteria = new Cdbcriteria ();
$criteria->order = ' news_id DESC ';
$criteria->condition = ' user_id = 1 ';
$count = News::model ()->count ($criteria);
$pages = new Cpagination ($count);
$pages->pagesize = 10;
$pages->applylimit ($criteria);
$list = News::model ()->findall ($criteria);
$this->render (' index ', array (' list ' => $list, ' pages ' => $pages));
}
View:
Copy Code code as follows:
<UL>
<?php foreach ($list as $item):?>
<LI>
<div class=page-header>
<?php echo $item--->news_title;?>
</DIV>
<div class=content>
<?php echo $item--->news_intro;?>
</DIV>
</LI>
<?php Endforeach;?>
</UL>
<div class=pagination>
<?php
$this--->widget (' clinkpager ', Array (
' Pages ' => $pages,
' Selectedpagecssclass ' => ' active ',//class of current page
' Hiddenpagecssclass ' => ' disabled ',//Disable page class
' Header ' => ',//content displayed before pagination
' Maxbuttoncount ' => 10,//Display paging quantity
' Htmloptions ' => Array (' class ' => '),
' Firstpagelabel ' => ' home ',
' Nextpagelabel ' => ' next page ',
' Prevpagelabel ' => ' on a page ',
' Lastpagelabel ' => ' last ',
)
);
?>
</DIV>
The third type: DAO implementation paging.
Controller layer:
Copy Code code as follows:
Public Function Actionreport ()
{
$sql = "Select Remitdate, sum (rate) Sumrate from Td_delivery
GROUP BY Remitdate
ORDER BY remitdate Desc ";
$criteria =new Cdbcriteria ();
$result = Yii::app ()->db->createcommand ($sql)->query ();
$pages =new cpagination ($result->rowcount);
$pages->pagesize=2;
$pages->applylimit ($criteria);
$result =yii::app ()->db->createcommand ($sql. " Limit:offset,:limit ");
$result->bindvalue (': Offset ', $pages->currentpage* $pages->pagesize);
$result->bindvalue (': Limit ', $pages->pagesize);
$posts = $result->query ();
$this->render (' a '), Array (
' Posts ' => $posts,
' Pages ' => $pages,
));
}
View Layer:
Copy Code code as follows:
<?php foreach ($posts as $row):?>
<?php Echo Chtml::link ($row ["Remitdate"],array (' Delivery/view ', ' remitdate ' => $row ["Sumrate"]));? >
<?php echo $row ["Sumrate"]. " <br/> "?>
<?php Endforeach;? >
<?php
Pagination Widget Code:
$this->widget (' Clinkpager ', Array (' pages ' => $pages));
?>
Advantage: DAO efficiency is high; Disadvantage: The view layer needs to write some of its own style, slightly more trouble
The fourth kind: The Widget realizes the paging
Model layer:
Copy Code code as follows:
/**
* @var string attribute: Daily freight (for statistics)
* Need to make a statement on the newly added fields
*/
Public $dayrate;
/*
* Statistic function: Statistics daily freight
*/
Public Function statistics ()
{
$criteria = new Cdbcriteria;
$criteria->select = ' remitdate, sum (rate) as dayrate ';
$criteria->group = ' remitdate ';
return new Cactivedataprovider (Get_class ($this), Array (
' Criteria ' => $criteria,
' Sort ' =>array (
Table header settings Click on the sorted field
' Attributes ' =>array (
' Remitdate ',
' Dayrate ' =>array (
' ASC ' => ' dayrate ',
' desc ' => ' dayrate desc ',
)
),
' Defaultorder ' => ' remitdate desc ',
),
));
}
Controller layer:
Copy Code code as follows:
/**
* Waybill Statistics Function:
* Statistics by date
*/
Public Function Actionreport ()
{
$model =new Delivery (' statistics ');
$model->unsetattributes (); Clear any default values
$this->render (' a '), Array (
' Model ' => $model,
));
}
View Layer:
Copy Code code as follows:
<?php $this->widget (' Zii.widgets.grid.CGridView ', Array (
' id ' => ' delivery-grid ',
' Dataprovider ' => $model->statistics (),
' Filter ' => $model,
' Columns ' =>array (
' Remitdate ',
' Dayrate ',
Array
' Class ' => ' Cbuttoncolumn ',
),
),
));
?>
Advantages: Can be used with the style; Disadvantage: The efficiency is slightly low.
Type Fifth: Ajax paging
Yii in the Ajax page has a variety of implementation methods, more traditional is in view to write JS to achieve, presumably is this:
In view JS probably logic is this
Copy Code code as follows:
$ (' #listview. Yiipager a '). Live (' click ', function () {
$.ajax ({
url:$ (This). attr (' href '),
Success:function (HTML) {
$ (' #listview '). HTML (HTML);
}
});
Return false;//block a label
});
Then the AJAX request is judged in controller, then the local list view is rendered using the RenderPartial method, and the local view is populated with the Ajax method in the view to the locally refreshed Div. The general logic of controller is:
Copy Code code as follows:
if (Yii::app ()->request->isajaxrequest) {
$this->renderpartial (' _comments ', Array (
' Model ' => $model,
' Comments ' => $comments,//foreach gets each piece of data in the local view
' Pages ' => $pages,
));
Yii::app ()->end ();
}
It was later found that the CListView in Yii was more convenient, encapsulated pagination, foreach display list, and data sorting. Specific can be found in Yii's API manual. Use CListView is the default Ajax paging, using the following methods:
In controller:
Copy Code code as follows:
$criteria = new Cdbcriteria ();
$criteria->order = ' create_time ' DESC ';
$dataProvider = new Cactivedataprovider (' Comments ', Array (
' Pagination ' =>array (
' PageSize ' =>yii::app ()->params[' Commentsperpage '],//set the number of pagination to determine the number of strips to fetch data
),
' Criteria ' => $criteria,
));
$this->render (' comments ', array (
' Model ' => $model,
' Dataprovider ' => $dataProvider,
));
And then in the view:
Copy Code code as follows:
<?php $this->widget (' Zii.widgets.CListView ', Array (
' Dataprovider ' => $dataProvider,
' Itemview ' => ' _comments ',
' Ajaxupdate ' => false,//so no Ajax pages
' Pager ' => Array (//pager configuration information. Default is <code>array (' class ' => ' Clinkpager ') </code>. You can also configure your own
' Nextpagelabel ' => ' next page» ',
' Prevpagelabel ' => ' «Previous page '
),
You can also configure some sort rules here, and you can consult the manual
));
?>
This realizes the Ajax paging, very convenient.
I hope this article will help you with the PHP program design based on the YII framework.