从yii2中的gii代码生成创建分页

时间:2016-08-28 17:16:28

标签: php pagination yii2

我使用Gii生成代码。

现在,我想将视图中的表显示为分页。 所以,我这样用:

CONTROLLER

Route::get('contact/my-details', 'Pages\ContactController@getMyDetails');

查看

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
    ]);
}

它给我这样的错误:

<?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'],
    ],
]);
?>

在gii2中实现分页的最佳实践,我也希望使用pjax来管理这个分页。

寻求帮助。非常感谢

2 个答案:

答案 0 :(得分:1)

您可以设置 echo "<div class='art'> <img class='pic' src='img/".$row["name"]."_tnail.jpg' alt='".$row["name"]." • ".$row["year"]." • ".$row["type"]."' height='auto' width='100%'/> <div class='modal'> <img class='big'/> <span class='titles'></span> </div> </div>" ; 的{​​{1}},如下所示:

pageSize

如果您在dataProvider之间有$dataProvider->pagination = [ 'pageSize' => 5, ]; Pjax::begin,则分页将自动使用Pjax。

有关详细信息,请参阅here

答案 1 :(得分:1)

感谢Kostas,它现在正在工作:

最终代码如下:

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,
    ]);
}