这是默认代码
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
现在我希望网格视图向我显示数组中的数据。我怎样才能做到这一点 ?这是否与gridView的dataprovider属性有关?
答案 0 :(得分:4)
只需在控制器/操作
中构建正确的dataProvider在这种情况下,您需要在Yii2指南
中显示arrayDataProvider并渲染例如
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,
]);
}