2017-08-07 82 views
1

我想在Yii 2中更改GridView的顯示格式。例如,我希望有一些行和列的數據,如Internet市場顯示產品。 例如,在每一行中我想要有4-5個產品.. 這是默認使用GridView的一個示例。如何在yii2中更改Gridview格式?

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

      'id', 
      'sim_num', 
      'network', 
      'twog_network', 
      'threeg_network', 
      // 'fourg_network', 
      // 'bady_struct', 
      // 'process', 
      // 'other:ntext', 
      // 'os', 
      // 'gesture', 
      // 'items', 
      // 'speaker', 

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

如何像HTML表格的格式顯示從數據庫中的數據,並確定在它分頁行數和列數?

謝謝;)

回答

2

GridView是用於以表格格式顯示數據。你在找什麼是ListView

這與GridView一樣支持分頁,您也使用數據提供程序。通過ListView,您可以將自己的「每個元素視圖」設置爲任何你喜歡的。

請參閱Guide中的文章。

+0

是否可以在列表視圖中添加過濾器進行搜索? – areff

+0

您正在使用數據提供程序進行篩選,所以是的,就像在GridView中一樣。不同之處在於過濾器表單不像GridView那樣與數據表相結合,您必須將其添加到ListView旁邊。 – Bizley

+0

謝謝:)讓我測試它 – areff

0

請參閱下面的示例。我希望它能幫助你。

<?= GridView::widget([ 
    'dataProvider' => $dataProvider, 
    'filterModel' => $searchModel, 
    'layout'=>"{items}\n{summary}\n{pager}", 
    'options' => ['style' => 'overflow-x:scroll;width:100%'], 
    'columns' => [ 
     [ 
      'class' => 'yii\grid\CheckboxColumn', 
      'checkboxOptions' => function($model, $key, $index, $column) { 
       return ['value' => $model->who_we_are_id]; 
      } 
     ], 

     [ 
      'attribute' => 'language_id', 
      'label' => 'Language', 
      'value' => 'languages.name', 
     ], 
     // 'description', 

     [ 
      'attribute'=>'image', 
      'label' => 'Image', 
      'content'=>function($data){ 
       $web_path = Yii::getAlias('@webroot'); 
       if(file_exists($web_path."/images/who_we_are/".$data->image) && !empty($data->image)){ 

        $url = \Yii::$app->request->BaseUrl.'/images/who_we_are/'.$data->image; 
        return Html::img($url,["width"=>"50px","height"=>"50px"]); 
       } 
      } 
     ], 
     'url', 
     [ 
      'attribute'=>'created_on', 
      'label' => 'Date & Time', 
      'content'=>function($data){ 
       if($data->created_on != '') 
        return date("m-d-Y H:i:s",strtotime($data->created_on)); 
      } 
     ], 

     ['class' => 'yii\grid\ActionColumn','template' => '{update}'], 
    ], 
]); ?>