Uzumaki Naruto
Uzumaki Naruto

Reputation: 753

how to show array data in kartik gridView

This is the default code

GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

Now I want the grid view to show me the data that I have in an array. HOw can I do that ? Is this have something to do with dataprovider property of the gridView ?

Upvotes: 3

Views: 2081

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

Simply build the proper dataProvider in your controller/action

In this case you need an arrayDataProvider like show in Yii2 guide

and render eg

    use yii\data\ArrayDataProvider;

    ......


    public function actionYourAction()
    { 

        $data = [
            ['id' => 1, 'name' => 'name 1', ...],
            ['id' => 2, 'name' => 'name 2', ...],
            ...
            ['id' => 100, 'name' => 'name 100', ...],
        ];

        $datProvider = new ArrayDataProvider([
            'allModels' => $data,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort' => [
                'attributes' => ['id', 'name'],
            ],
        ]);


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

Upvotes: 4

Related Questions