1 php 後端php
public function actionPage() {
$data = User::find(); //User爲model層,在控制器剛開始use了field這個model,這兒能夠直接寫Field,開頭大小寫均可以,爲了規範,我寫的是大寫
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '30']); //實例化分頁類,帶上參數(總條數,每頁顯示條數)
$model=$data->offset($pages->offset)->limit($pages->limit)->all();
$is_ajax = Yii::$app->request->isAjax ;
if(!$is_ajax){
return $this->render('show',[
'datas' => $model ,
'pages' => $pages,
]);exit;
}
$string = '' ;
foreach ($model as $p){
$string .='<li>'.$p['email'].'</li>';
}
echo json_encode(['datas' => $string,'pages' => LinkPager::widget(['pagination' => $pages])]);
}html
2 模板jquery
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
use yii\helpers\Url;
use yii\web\View;
?>web
<html>
<?=Html::jsFile('@web/js/jquery-2.1.0.js')?>
<style>
* {
margin: 0;
border: 0;
padding: 0;
font-size: 13pt;
}
#page {
height: 40px;
border-top: #060 2px solid;
border-bottom: #060 2px solid;
background-color: #690;
}
#page ul {
list-style: none;
margin-left: 50px;
}
#page li {
display: inline;
line-height: 40px;
}
#page a {
color: #fff;
text-decoration: none;
padding: 20px 20px;
}
#page a:hover {
background-color: #060;
}
</style>
<body>
<div>
<ul id="lists">
<?php foreach( $datas as $p) { ?>
<li><?php echo $p['email'];?></li>
<?php }?>
</ul>
</div>
<div id="page">
<?=LinkPager::widget(['pagination' => $pages,]);?>
</div>ajax
<script>json
$(function(){後端
$(document).on('click','.pagination a',function(e){
e.preventDefault();
var url = $(this).attr('href');
$.get(url,function(msg){
//alert(msg);
$('#lists').html(msg.datas);
$("#page").html(msg.pages);
},'json');
});
});
</script>
</body>
</html>app