5

I have an array:

$arr = ['ID' => 'A1', 'Description' => 'Item to be sold', ...]

In controller:

$provider = new ArrayDataProvider([
'allModels' => $arr,
//'sort' =>['attributes' => ['ID', 'Description'],],
'pagination' => ['pageSize' => 5]
]);
$this -> render('index', ['provider' => $arr]);

In view (index.php):

GridView::widget([
'dataProvider' => $provider,
]);

And there is no result on page. Where it is wrong?

1 Answer 1

7

There are few errors in your code.

1) $arr should have the structure like this:

$arr = [
    ['ID' => 'A1', 'Description' => 'Item to be sold'],
    ...
],

2) In the render parameters you passed $arr instead of $provider, should be:

$this->render('index', ['provider' => $provider]);

3) You missed return statement before render:

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

Also I don't recommend using spaces around the arrow.

4) You did not specified any columns in GridView. You can add ID and Description like this:

GridView::widget([
    'dataProvider' => $provider,
    'columns' => [
        'ID',
        'Description',
    ],
]);

5) And finally you are not echoing the GridView to the screen. Should be:

echo GridView::widget([...]);

or

<?= GridView::widget([...]) ?>
Sign up to request clarification or add additional context in comments.

2 Comments

You are right, but how can you filter columns with the GridView ?
If you have another problem, please create the separate question for that. This problem is solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.