areff
areff

Reputation: 145

How to change Gridview format in yii2?

I want to change display format of GridView in Yii 2. For example I want to have some rows and columns of data like Internet markets displaying products. For example in each row I want to have 4-5 products.. Here is an example of using GridView by default.

    <?= 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'],
        ],
    ]); ?>

How to show data from database in a format like table html and determine rows and columns number with pagination in it??

Thanks ;)

Upvotes: 1

Views: 3646

Answers (2)

Naushil Jain
Naushil Jain

Reputation: 444

Refer Below Example. I hope it will help you.

 <?= 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}'],
    ],
]); ?>

Upvotes: 0

Bizley
Bizley

Reputation: 18021

GridView is for displaying data in a table format. What you are looking for is ListView.

This supports pagination just like GridView, you are using data provider as well. With ListView you can set your own "each-element view" to be whatever you like.

See the article in the Guide.

Upvotes: 2

Related Questions