GridView в Pjax (Yii2)

← вернуться к статье

Controller

<?php
public function actionPjaxExample7()
    {
        $array = [
            ['id'=>1, 'name'=>'Sam','age'=> '21', 'height'=> '190'],
            ['id'=>2, 'name'=>'John','age'=> '34', 'height'=> '156'],
            ['id'=>3, 'name'=>'Alex','age'=> '29', 'height'=> '178'],
            ['id'=>4, 'name'=>'David','age'=> '31', 'height'=> '188'],
            ['id'=>5, 'name'=>'Max','age'=> '26', 'height'=> '184'],
        ];

        $searchModel = [
            'age' => Yii::$app->request->getQueryParam('filterage', ''),
        ];

        $filteredData = array_filter($array, function($item) use ($searchModel) {
            if (!empty($searchModel['age'])) {
                if ($item['age'] == $searchModel['age']) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        });

        $dataProvider = new \yii\data\ArrayDataProvider([
            'key' => 'id',
            'allModels' => $filteredData,
            'sort' => [
                'attributes' => ['name'],
            ],
            'pagination' => [
                'pageSize' => 3,
            ],
        ]);

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

View

<?php Pjax::begin(); ?>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            'id',
            [
                'attribute' => 'name',
                'value' => 'name',
            ],
            [
                'attribute' => 'age',
                'filter' => '<input class="form-control" name="filterage" value="'. $searchModel['age'] .'" type="text">',
                'value' => 'age',
            ],
            'height:ntext',
        ],
    ]); ?>
<?php Pjax::end(); ?>

Пример

Показаны записи 1-3 из 5.
IdNameAgeHeight
   
1Sam21190
2John34156
3Alex29178
comments powered by HyperComments