Skip to content

Instantly share code, notes, and snippets.

@c006
Created January 4, 2016 01:52
Show Gist options
  • Save c006/33f7c24ffeb17a102ceb to your computer and use it in GitHub Desktop.
Save c006/33f7c24ffeb17a102ceb to your computer and use it in GitHub Desktop.
Yii2 GirdView example
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'showHeader' => TRUE,
'emptyCell' => '-',
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Image',
'format' => 'raw',
'value' => function ($data) {
$url = "/images/products/" . $data['image'];
return Html::img($url, ['alt' => $data['name'], 'height' => 50]);
},
],
'model',
'name',
'qty',
'price',
[
'label' => 'Discount',
'format' => 'raw',
'value' => function ($data) {
return ($data['discount']) ? $data['discount'] . '% Off' : '';
},
],
[
'label' => 'Auto Ship',
'format' => 'raw',
'value' => function ($data) {
$auto_ship = ModelHelper::getAutoShipLinkName($data['auto_ship']);
return (sizeof($auto_ship)) ? $auto_ship['duration'] . ' ' . $auto_ship['type'] : '';
},
],
[
'label' => 'Totals',
'format' => 'raw',
'value' => function ($data) {
return '$' . number_format($data['price'] * $data['qty'], 2);
},
],
['class' => 'yii\grid\ActionColumn',
'template' => '',
],
],
]); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment