CloudSeph
CloudSeph

Reputation: 883

how to set the amount of items to be shown in yii2 grid view?

enter image description here

Controller Code:

 public function actionIndex() {
        $searchModel = new ProductSearch();
        if (Yii::$app->request->get('per-page') !== NULL) {
            $searchModel->_pageSize = Yii::$app->request->get('per-page');
        }
        if ($searchModel->load(Yii::$app->request->post())) {
            $searchModel->_pageSize = intval(Yii::$app->request->post()['ProductSearch']['_pageSize']);
            $_SESSION['page_size'] = $searchModel->_pageSize;
        }


            if (isset($_SESSION['page_size']))
            {
                $dataProvider = $searchModel->search(Yii::$app->request->queryParams, null ,$_SESSION['page_size']);
            }
            else
            {
                $dataProvider = $searchModel->search(Yii::$app->request->queryParams, null, 20);
            }
            $dataProvider->pagination->pageSize=10;
            $this->layout = 'main_ecommerce';
            return $this->render('index', [
                        'searchModel' => $searchModel,
                        'dataProvider' => $dataProvider,

            ]);

View code:

if (isset($_SESSION['page_size']))
                {
                $searchModel->_pageSize = $_SESSION['page_size'];
                }
                Pjax::begin();
                $form = ActiveForm::begin();
                echo $form->field($searchModel, '_pageSize', [
                            'inputOptions' => [
                                    'placeholder' => 'records',
                                    'style' => 'overflow-x: auto;',
                                    'class' => 'form-control input-xsmall input-inline' ],
                        ])->label(Yii::t('app', 'Records : '))
                        ->dropDownList(['10'=> 10, '20' => 20, '50' => 50], ['onchange'=>"this.form.submit();"]);
                ActiveForm::end();
                Pjax::end();
                echo $searchModel->_pageSize;
                ?>
             <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,                 
        'pager' => [
            'options'=>['class'=>'pagination'],   
            'prevPageLabel' => Html::tag('i', "", ['class' => 'fa fa-angle-left']),   
            'nextPageLabel' => Html::tag('i', "", ['class' => 'fa fa-angle-right']),   
            'firstPageLabel'=>Html::tag('i', "", ['class' => 'fa fa-angle-double-left']),  
            'lastPageLabel'=>  Html::tag('i', "", ['class' => 'fa fa-angle-double-right']),   
        ],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

The picture shows that the GridView is only displaying 30 rows of records. How can i adjust this so that i can display, for example 50 records per page. Somehow once i set it already, it keeps showing a value less than the one i set. Why is that?

Upvotes: 2

Views: 3088

Answers (2)

ScaisEdge
ScaisEdge

Reputation: 133360

Try simply

  public function actionIndex() {

      $searchModel =  new ProductSearch();
      $dataProvider = $searchModel->search(Yii::$app->request->queryParams);


        $dataProvider->pagination->pageSize=10;
        $this->layout = 'main_ecommerce';
        return $this->render('index', [
                    'searchModel' => $searchModel,
                    'dataProvider' => $dataProvider,

        ]);

and for view

  <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,                 
    'pager' => [
        'options'=>['class'=>'pagination'],   
        'prevPageLabel' => Html::tag('i', "", ['class' => 'fa fa-angle-left']),   
        'nextPageLabel' => Html::tag('i', "", ['class' => 'fa fa-angle-right']),   
        'firstPageLabel'=>Html::tag('i', "", ['class' => 'fa fa-angle-double-left']),  
        'lastPageLabel'=>  Html::tag('i', "", ['class' => 'fa fa-angle-double-right']),   
    ],
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

Upvotes: 0

rushil
rushil

Reputation: 576

Try using

$dataProvider->pagination->pageSize=10;

in actionIndex before you return searchmodel and dataprovider

for example :

    $searchModel = new UserMasterSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->post());

    $dataProvider->pagination->pageSize=10;

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);

Upvotes: 1

Related Questions