Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customise grid view in yii2

How to remove summary and sorter for a particular grid view in Yii2. In Yii1.1 we can do that by setting the template property. In yii2 how to achieve this?

like image 676
Dency G B Avatar asked Feb 10 '14 10:02

Dency G B


People also ask

What is Grid view yii2?

The GridView widget takes data from a data provider and presents data in the form of a table. Each row of the table represents a single data item, and a column represents an attribute of the item.

How to show data in yii2?

use yii\grid\GridView; use yii\data\ActiveDataProvider; $dataProvider = new ActiveDataProvider([ 'query' => Post::find(), 'pagination' => [ 'pageSize' => 20, ], ]); echo GridView::widget([ 'dataProvider' => $dataProvider, ]);


2 Answers

To change only summary option, you can use:

'summary' => "{begin} - {end} {count} {totalCount} {page} {pageCount}", 

Then, if you want to empty summary set with empty string value like:

'summary'=> "", 

and to change layouts you can use:

'layout'=> "{summary}\n{items}\n{pager}" 

Then, if you want to empty layouts set layout with empty string value like:

'layout'=> "", 

Ref link

Ref link

So, for sample, i think bellow sample code can be help know how to change and custome GridView table in Yii 2:

                <?= \yii\grid\GridView::widget([                 'id' => 'table',                 'dataProvider' => $dataProvider,                 'layout'=>"{sorter}\n{pager}\n{summary}\n{items}",                 'summary' => "Showing {begin} - {end} of {totalCount} items",                 'tableOptions' => ['class' => 'table  table-bordered table-hover'],                 'rowOptions' => function ($model, $key, $index, $grid) {                     return [                         'style' => "cursor: pointer",                         'onclick' => 'location.href="'                             . Yii::$app->urlManager->createUrl('test/index')                             . '?id="+(this.id);',                     ];                 },                 'columns' => [                     [                         'class' => 'yii\grid\SerialColumn',                         'contentOptions' => ['style' => 'width: 20px;', 'class' => 'text-center'],                     ],                     [                         'class' => 'yii\grid\DataColumn',                         'attribute' => 'date',                         'headerOptions' => ['class' => 'text-center'],                         'label' => 'Date',                         'contentOptions' => ['style' => 'width: 130px;', 'class' => 'text-center'],                     ],                         'template' => '{view}',                         'buttons' => [                             'view' => function ($url, $model) {                                 return \yii\helpers\Html::a('<div class="text-center"><em data-toggle="tooltip"                                                             data-placement="top" title="more detail"                                                             class="fa fa-external-link-square text-warning"></em></div>',                                     (new yii\grid\ActionColumn())->createUrl('test/index', $model, $model['id'], 1), [                                         'title' => Yii::t('yii', 'view'),                                         'data-method' => 'post',                                         'data-pjax' => '0',                                     ]);                             },                         ]                     ],                 ],             ]); ?> 
like image 142
Sajjad Dehghani Avatar answered Sep 28 '22 02:09

Sajjad Dehghani


Got it.By setting the layout property,we can achieve it.

'layout'=>"{summary}\n{items}\n{pager}" 
like image 37
Dency G B Avatar answered Sep 28 '22 03:09

Dency G B