如何使用ActiveDataprovider在Yii2 GridView中显示数组数据

时间:2019-02-23 18:25:55

标签: php arrays gridview yii2

我有一系列与此非常相似的数据

$array = [
  'FUNDSCTR' => '10000001' 
  'RCMMTITEM' => 'R400001'
  'YEAR' => '2018'
  'CONSUMA' => 5898257
  'CONSUME' => 30140
  'AVAIL' => 5868117 ]

并在ArrayDataProvider中使用

 $dataProvider = new ArrayDataProvider(['allModels' => $array,]);

gridview中的代码

 <?= GridView::widget([
         'dataProvider' => $dataProvider,
         'columns' => [
                      ['class' => 'kartik\grid\SerialColumn'],
                        [
                            'label' => 'Year',
                            'attribute'=>'YEAR',
                        ],
                        [
                            'label' => 'Fund',
                            'attribute'=>'FUNDSCTR',
                        ],
                         [
                            'label' => 'Item',
                            'attribute'=>'RCMMTITEM',
                        ],
                         [
                            'label' => 'Consumeable',
                            'attribute'=>'CONSUMA',
                        ],
                         [
                            'label' => 'Consumed',
                            'attribute'=>'CONSUME',
                        ],
                        [
                            'attribute'=>'Available',
                            'value'=> 'AVAIL',
                        ],
                    ]);
                ?>
      </div>

荡妇 enter image description here

print_r($ dataProvider); 〜$ arrays返回

yii\data\ArrayDataProvider Object ( [key] => [allModels] => Array ( [RFUNDSCTR] => 10000001 [RCMMTITEM] => R400013 [RYEAR] => 2018 [CONSUMA] => 5898257 [CONSUME] => 30140 [AVAIL] => 5868117 )

我需要在gridView中显示这样的数据。

enter image description here

1 个答案:

答案 0 :(得分:2)

要正常工作,传递给ArrayDataProvider的数组应该是二维的,如下所示:

$array = [
    [
        'FUNDSCTR' => '10000001',
        'RCMMTITEM' => 'R400001',
        'YEAR' => '2018',
        'CONSUMA' => 5898257,
        'CONSUME' => 30140,
        'AVAIL' => 5868117,
    ],
    [
        'FUNDSCTR' => '10000001',
        'RCMMTITEM' => 'R400001',
        'YEAR' => '2018',
        'CONSUMA' => 5898257,
        'CONSUME' => 30140,
        'AVAIL' => 5868117,
    ],
    [
        'FUNDSCTR' => '10000001',
        'RCMMTITEM' => 'R400001',
        'YEAR' => '2018',
        'CONSUMA' => 5898257,
        'CONSUME' => 30140,
        'AVAIL' => 5868117,
    ]
];