dongqiang2024
2015-12-31 07:45
浏览 41
已采纳

Yii2-advanced:ArrayDataProvider

I want to use ArrayDataProvider with the following code of siteController. I wrote the following code but it doesn't work. Here is my actionIndex :

public function actionIndex()
{
        $query = new \yii\db\Query;
        $query->select('*')->from('business_main_categories');
        $query->createCommand();

        $dataProviders = [];

        foreach ($query as $category) {
            $dataProviders[] = new ArrayDataProvider([
                'allModels' => $category,
                'sort' => false,
                'pagination' => false,
            ]);
        }

        return $this->render('index', [
            'dataProvider' => $dataProviders,
        ]);
}

And want it to iterate in gridView. So, I wrote the following code (I don't know whether it's correct or not) :

Here is my index.php :

<?php
    $dataProviders[] = 'dataProvider';
    foreach ($dataProviders as $dataProvider) {
        echo GridView::widget([
            'dataProvider' => $dataProvider,
            'summary' => '',
            'columns' => [
                [
                        'attribute' => 'bmc_image',
                        'format' => 'html',
                        'label' => '',
                        'value' => function ($data) {
                            return Html::img($data['bmc_image'],
                                ['width' => '210px', 'height' => '190px']);
                        },
                    ],
                ]
       ]); 
    } 
?>

图片转代码服务由CSDN问答提供 功能建议

我想将 ArrayDataProvider 与siteController的以下代码一起使用。 我写了下面的代码,但它不起作用。 这是我的actionIndex:

  public function actionIndex()
 {
 $ query = new \ yii \ db \ Query; 
 $ query-&gt; select(  '*') - &gt; from('business_main_categories'); 
 $ query-&gt; createCommand(); 
 
 $ dataProviders = []; 
 
 foreach($ query as $ category){
  $ dataProviders [] = new ArrayDataProvider([
'allModels'=&gt; $ category,
'sort'=&gt; false,
'pagination'=&gt; false,
]); 
} 
  
返回$ this-&gt; render('index',[
'dataProvider'=&gt; $ dataProviders,
]); 
} 
   
 
 

并希望它在gridView中迭代。 所以,我写了下面的代码(我不知道它是否正确):

这是我的index.php:

 <  code>&lt;?php 
 $ dataProviders [] ='dataProvider'; 
 foreach($ dataProviders as $ dataProvider){
 echo GridView :: widget([
'dataProvider'=&gt; $ dataProvider,
  'summary'=&gt;'',
'列'=&gt; [
 [
'属性'=&gt;'bmc_image',
'格式'=&gt;'html',
'标签'  =&gt;'',
'值'=&gt;函数($ data){
返回Html :: img($ data ['bmc_image'],
 ['width'=&gt;'210px','  height'=&gt;'190px']); 
},
],
] 
]);  
} 
?&gt; 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dq_609586475 2016-01-04 07:33
    已采纳

    I solved my problem without using gridview. As follows -

    My SiteController -

    public function actionIndex()
    {
            $searchModel = new BusinessMainCategoriesSearch();
            $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
            $dataProvider->pagination->pageSize = $dataProvider->getTotalCount();   //-1 : disable
    
            return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            ]);
    }
    

    using this code I get all records in dataProvider from my db. (Note that, I'm using ActiveDataProvider in my 'BusinessMainCategoriesSearch' model)

    And, my index.php is -

    <?php
        $m = $dataProvider->getModels();
        foreach ($m as $dp) {
            echo "<img src = '".$dp['bmc_image']."' />";
            echo '<center><font color = "white">'.$dp['bmc_name'].'<font/></center>';
        } 
    ?>
    

    It worked great for me & it's a easiest way to do so.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duannao3402 2015-12-31 08:48

    Controller

    public function actionIndex()
    {
        $query = new \yii\db\Query;
        $dataProvider = new ArrayDataProvider([
        'allModels' =>$query->from('business_main_categories')->all(),
        ]);
    
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }
    

    Index

    <?= GridView::widget([
            'dataProvider' => $dataProvider,
                'columns' => [
                ['class' => 'yii\grid\SerialColumn'],
                 [
                  'attribute' => 'bmc_image',
                  'format' => 'html',               
                   'value' => function ($data) {
                        return Html::img($data['bmc_image'],['width' => '210px', 'height' => '190px']);
                       },
                 ],
            ],
    ]); ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题