beginner
beginner

Reputation: 2032

YII2 insert icon in the gridview

In the gridview, there is a date column. There are times that the date is blank. What I want is if the date is blank, it will display and "x" icon in red color and if there is date in it, it will display a check icon in green color. How is that done in the gridview?

Here is the code so far...

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute'=>'province',
                'value'=>'incomingSp.brgyCode.cityCode.province.prov_name',
            ],
            [
                'attribute'=>'city',
                'value'=>'incomingSp.brgyCode.cityCode.city_name',
            ],
            [
                'attribute'=>'brgy',
                'value'=>'incomingSp.brgyCode.brgy_name',
            ],
            [
                'attribute'=>'title',
                'value'=>'incomingSp.sp_title',
            ],

            'incoming_sp_id',
            'date_encoded',
            'encoded_by',
            ['class' => 'yii\grid\ActionColumn',
                'template'    => '{view}{update}{delete}',
            ],
        ],
    ]); ?>

Upvotes: 3

Views: 4353

Answers (2)

Yundong Cai
Yundong Cai

Reputation: 79

I set "format"=>"html", then can show it as Icon rather than the raw text

Upvotes: 1

arogachev
arogachev

Reputation: 33538

Extend date_encoded column declaration like this:

[
    'attribute' => 'date_encoded',
    'format' => 'raw',
    'value' => function ($model) {
        if ($model->date_encoded === null) {
            return 'x'; // "x" icon in red color
        } else {
            return 'v'; // check icon 
        }
    },
],

Official docs:

Upvotes: 2

Related Questions