Reputation: 2226
I have use Gii to generate my code.
Now, I want to display the table in view into pagination. So, I use like this :
CONTROLLER
public function actionIndex() {
$searchModel = new BarangSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$countDataProvider = clone $dataProvider;
$paging = new \yii\data\Pagination([
'totalCount' => $countDataProvider->count(),
'defaultPageSize' => 5
]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'paging' => $paging
]);
}
VIEW
<?php
Pjax::begin([
'timeout' => 5000,
'id' => 'pjax-gridview'
]);
?>
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'NO_URUT',
'CONSIGNEE',
'CONTAINER',
'SIZE',
'COIL_NO',
'NET',
'GROSS',
'CONTRACT_NO',
'KET',
'NAMA_FILE',
'TGL_UNSTUFF',
'CREATED_AT',
'UPDATED_AT',
[
'attribute' => 'CREATED_BY',
'value' => function($data) {
$username_created_by = $data->CREATED_BY;
if ($user = User::findIdentity($data->CREATED_BY)):
$username_created_by = $user->username;
endif;
}
],
'UPDATED_BY',
['class' => 'yii\grid\ActionColumn'],
],
]);
?>
It gives me error like this :
Calling unknown method: yii\data\ActiveDataProvider::count()
What best practice to implement pagination in gii2, and I want to use pjax to manage this pagination too.
For the help. it so appreciated
Upvotes: 0
Views: 607
Reputation: 2226
Thanks Kostas, it working now :
The final code like this :
public function actionIndex() {
$searchModel = new BarangSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination = [
'pageSize' => 2,
];
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Upvotes: 1
Reputation: 4747
You can set the pageSize
of the dataProvider
like this:
$dataProvider->pagination = [
'pageSize' => 5,
];
If you have Pjax::begin
and Pjax::end
between the GridView
then pagination will use Pjax automatically.
For more details you can see here.
Upvotes: 1