Amjad Sarwar
Amjad Sarwar

Reputation: 141

How to user if condition in yii2 gridview active button or deactive botton show on gridview

How to user if condition in yii2 gridview active button or deactive button show on gridview

     <div class="report-index">
    <?= GridView::widget([
    'dataProvider' => $dataProvider,
    'layout' => "{summary}\n{items}",
     'columns' => [
    ['class' => 'yii\grid\SerialColumn'],
       'username',
        'email',
        'status',
     [
    'attribute'=>'created_at',
    'format' => ['date', 'php:d.m.Y'],
    ],
    'active',
      [
      'class' => 'yii\grid\ActionColumn',
      'header' => 'Action',
      'template' => '{view}{delete}',
      'buttons' => [
        'view' => function ($url, $model,$key) {

            return Html::a('<span class="glyphicon glyphicon-eye-open">           </span>', $url, [
                        'title' => Yii::t('app', 'View'),
            ]);
        },

         'delete' => function ($url, $model,$key) {

            return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, [
                        'title' => Yii::t('app', 'Delete'),
            ]);
        },

      ],

     ],

    ],

]); ?>

    <div id="custom-pagination">
    <?php

      echo LinkPager::widget([
'pagination' => $pages,
     );

    ?>

How to user if condition in yii2 gridview active button or deactive button show on gridview i use two button when active is 1 show green button if active record 0 show red button

how to user if condition in yii2 gridview active button or deactive button show on gridview i use two button when active is 1 show green button if active record 0 show red button

Upvotes: 2

Views: 8454

Answers (1)

Sohel Ahmed Mesaniya
Sohel Ahmed Mesaniya

Reputation: 3450

You have add a anonymous function to value with signature function($model, $key, $index) reference

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'layout' => "{summary}\n{items}",
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        'username',
        'email',
        'status',
        [
            'attribute'=>'created_at',
            'format' => ['date', 'php:d.m.Y'],
        ],
        [
            'attribute'=>'active',
            'header'=>'Status',
            'filter' => ['Y'=>'Active', 'N'=>'Deactive'],
            'format'=>'raw',    
            'value' => function($model, $key, $index)
            {   
                if($model->is_active == 'Y')
                {
                    return '<button class="btn green">Y</button>';
                }
                else
                {   
                    return '<button class="btn red">N</button>';
                }
            },
        ],
        [
            'class' => 'yii\grid\ActionColumn',
            'header' => 'Action',
            'template' => '{view}{delete}',
            ...

Upvotes: 7

Related Questions