2019独角兽企业重金招聘Python工程师标准>>>
1 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 .='
}
echo json_encode(['datas' => $string,'pages' => LinkPager::widget(['pagination' => $pages])]);
}
2 模板
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\LinkPager;
use yii\helpers\Url;
use yii\web\View;
?>
=Html::jsFile('@web/js/jquery-2.1.0.js')?>
=LinkPager::widget(['pagination' => $pages,]);?>
$(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');
});
});