代码之家  ›  专栏  ›  技术社区  ›  Beginner m latha

如何使用ActiveDataProvider在yii2网格视图中显示数组数据?

  •  -1
  • Beginner m latha  · 技术社区  · 6 年前

    在我的数据库中,

    "email" : [ 
        "amnop@mailinator.com", 
        "abc@mail.com"
    ],
    

    当我 print_r($model->email) ,请 它显示

    Array ( [0] => amnop@mailinator.com [1] => abc@mail.com )
    

    在我的网格视图中,

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
            ----
            'columns' => [
                         -----
                        'price',
                        [
                             'attribute' => 'email',
                              'value' => function($model) {
                                  //I need help here... I prefer any foreach function
                            }
                        ],
    
                        -----
            ]
    ?>
    

    我必须在同一列中显示所有电子邮件。怎么做?

    编辑

    我使用 ActiveDataprovider 当我从数据库中获取值时。

    2 回复  |  直到 6 年前
        1
  •  2
  •   rob006    6 年前

    [
         'attribute' => 'email',
         'value' => function($model) {
              if (is_array($model->email)) {
                  return implode(', ', $model->email);
              }
    
              return $model->email;
        }
    ],
    
        2
  •  0
  •   ScaisEdge    6 年前

    $data = [
        ['email' => 'amnop@mailinator.com'],
        ['email' => 'abc@mail.com'],
        ...
        ['email' =>  'youremail100@mail.com'],
    ];
    

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

      <?= GridView::widget([
              'dataProvider' => $dataProvider,
              ----
              'columns' => [
                           -----
                          'price',
                          [
                               'attribute' => 'email',
                                'value' => function($model) {
                                    //I need help here...
                              }
                          ],
    
                          -----
              ]
      ?>
    

    https://www.yiiframework.com/doc/guide/2.0/en/output-data-providers https://www.yiiframework.com/doc/api/2.0/yii-data-arraydataprovider