如何在kartik gridView中显示数组数据

时间:2015-07-26 04:51:10

标签: gridview yii2

这是默认代码

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

现在我希望网格视图向我显示数组中的数据。我怎样才能做到这一点 ?这是否与gridView的dataprovider属性有关?

1 个答案:

答案 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,
        ]);
    }