yii2的分頁和ajax分頁

2022-06-25 04:12:12 字數 1211 閱讀 6207

要想使用yii分頁類

第一步:在控制器層載入分頁類

use yii\data\pagination;

第二步: 使用model層查詢資料,並用分分頁,限制每頁的顯示條數

$data = user::find();  //user為model層,在控制器剛開始use了field這個model,這兒可以直接寫field,開頭大小寫都可以,為了規範,我寫的是大寫

$pages = new pagination(['totalcount' =>$data->count(), 'pagesize' => '3']);    //例項化分頁類,帶上引數(總條數,每頁顯示條數)

$model=$data->offset($pages->offset)->limit($pages->limit)

->all();

第三步:向檢視層傳送資料

return $this->render('show',[

'pages' => $pages,

]);傳送資料的時候,用render函式,否則過去檢視層沒有樣式

第四步:

在檢視層頂層寫上

<?php

use yii\helpers\html;

use yii\widgets\activeform;

use yii\widgets\linkpager;

use yii\helpers\url;

?>

在顯示資料底部

<?=

linkpager::widget([

'pagination' => $pages,

]);?>

效果:yii框架裡面的ajax分頁,其實很簡單,

只需在<?=

linkpager::widget([

'pagination' => $pages,

]);?>

後面加上下面這段**:

<?php $this->beginblock('abc'); ?>

$(document).on('click','.pagination a',function(e));

});<?php $this->endblock();  $this->registerjs($this->blocks['abc'], view::pos_end); ?>

另外在檢視層頂部加上這一句話

use yii\web\view;

最後的效果和上面的效果是一樣的!

yii2 分頁 和 ajax分頁

分 頁 在寫分頁之前,你需要在控制器引入use yii data pagination 物件為其填充資料,然後在進行下面的操作。public function actionshow 上面就是控制器的 接下來就該寫檢視層了。同樣在寫檢視層時要引入 use yii widgets linkpager p...

YII2 搜尋帶分頁,分頁的兩種方式

1.文章表關聯 other code 關聯 public function getcate 2.搜尋模型 common models search 建立articlesearch.php namespace common models search use yii use yii base mode...

yii2 分頁 redis儲存

public function actionshow return this render show models models,pages pages,msg 資料庫取值 else return this render show models b,pages pages,msg redis取值 上...