如何在yii2中显示gridview

时间:2016-03-23 09:04:30

标签: php gridview yii2

以下代码用于显示gridview,但它显示"数据提供者"错误。

模型:

Subcategory.php:

public function getCountries()
 {
     return $this->hasOne(Subcategories::className(),['id'=>'catid']);
 }

查看:

subcategory.php:  我添加了以下编码,以便在网格视图中查看已保存的数据。

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns'=>[
    ['class'=>'yii\grid\SerialColumn'],
    'id',
    'categoryname',
    [
        'attribute' => subcategory,
        'value'=>getCountries.subcatname,
    ],
    ['class'=>'yii\grid\ActionColumn'],
],
]); ?>

CONTROLLER

public function actionView2()
{
  $dataProvider = new ActiveDataProvider([
      'query' => Subcategory::find(),
      'pagination' => [
          'pageSize' => 20
       ]
   ]);
   $posts = $dataProvider->getModels();
   return $this->render('subcategory' [
       'dataProvider' => $dataProvider,
   ]);
}

我已应用此编码但出现以下错误:

PHP Notice – yii\base\ErrorException Undefined variable: dataProvider

0 个答案:

没有答案