如何在GridView中显示json格式化数据

时间:2016-09-15 10:06:26

标签: yii2

我想在gridview中显示json格式化数据。我有以下内容:

Json数据:

[{"c_id":1,"c_name":"Marokand","c_logo":"Marokand","c_slogan":"Marokand","c_address":"Tashkent Amir Temur, Uzbekistan","c_website":"www.maroqand.uz","c_email":"info@maroqand.uz","c_owner":"someone",},{"c_id":2,"c_name":"Apple LLC","c_logo":"","c_slogan":"","c_address":"","c_website":"","c_email":"","c_owner":""}

在我的视图文件中,我有以下代码:

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

            'c_id',
            'c_name',
            'c_logo',
            'c_slogan',
            'c_address',
            // 'c_website',
            // 'c_email:email',
            // 'c_owner',
            // 'c_short_description:ntext',
            // 'c_description:ntext',
            // 'c_history:ntext',
            // 'c_goal:ntext',
            // 'cci_memerbship_id',

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

如何在gridview中显示json数据?

1 个答案:

答案 0 :(得分:0)

使用ArrayDataProvider:Refer Link

在控制器文件上首先解码像这样的json数据和init数据提供者:

use yii\data\ArrayDataProvider;
use yii\helpers\Json;

$data = Json::decode("YOUR JOSN DATA", true);

$dataProvider = new ArrayDataProvider([
  'allModels' => $data,
  'pagination' => [
  'pageSize' => 10,
],
  'sort' => [
    'attributes' => ['c_id'],
  ],
]);

在观点方面:

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

        'c_id',
        'c_name',
        'c_logo',
        'c_slogan',
        'c_address',
        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>