How to insert column status in kartik gridview

Hi everyone, I have two columns on which the associated IP for each PC number. The associated IP column is related to another model. How can I insert in another column named for example Status the function that checks if the associated IP field is empty or not. If empty, you must return the value “Available” otherwise “Not available”. The associated IP field is INT (5). I use the kartik / gridview extension and I have a great time.

You can try something like this,

[
     'header' => 'Status', 
     'value' => function ($model) { 
        return !empty($model->associateIpRelation->associateIpFieldName) ? 'Not Available' : 'Available';
     },
],
1 Like

I have tried with this in gridview, but not change result:

    [
        'header' => 'Status',
        'attribute' => 'status',
        'value' => function ($model) {
            return !empty($model->ip_ass->status) ? 'Not Available' : 'Available';
        },
    ],

Hi, can you show what type is the status?

'value' => function ($model) {
    var_dump($model->ip_ass->status); die();
    return !empty($model->ip_ass->status) ? 'Not Available' : 'Available';
 },

If $model->ip_ass->status is an OBJECT you will be always get ‘Available’, or status has a value which not equals to ‘0’ (zero).
it will be correctly to check status like this:

$model->ip_ass->status == IpAssciateModel::STATUS_NOT_AVAILABLE ? 'Not Available': 'Available';

Hi, @stitch my table “numeri” is this:

table_numeri

In app\views\index.php:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        //'formatter' => ['class' => 'yii\i18n\Formatter', 'nullDisplay' => SELF_INACTIVE],
        'responsive' => true,
        'hover' => true,
        'resizableColumns' => true,
        'pjax' => true,
        'bordered' => true,
        'striped' => true,
        'condensed' => true,
        'panel' => [
            'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-th-list"></i> Numerazioni PC</h3>',
            'type' => GridView::TYPE_PRIMARY,
            'before' => '<i class="glyphicon glyphicon-asterisk"></i>  per la ricerca utilizzare i campi disponibili e premere invio',
            'after' => '{pager}',
            'footer' => false,
        ],
        'export' => [
            'fontAwesome' => false,
            'itemsAfter'=> [
                '<li role="presentation" class="divider"></li>',
                '<li class="dropdown-header">Esporta tutti i dati</li>',
                $fullExportMenu
            ]
        ],
        'columns' => [
            ['class' => 'kartik\grid\SerialColumn'],

            //'id_numeri',
            'elenconumeri',
            [
                'attribute' => 'ipass',
                'value' => 'gestionalepc.elencoip',
            ],
            [
                'attribute' => 'status',
                'value' => static function ($model) {
                    return empty($model->ipass->status) ? 'Non disponibile' : 'Disponibile';
                },

            ],
            'created_at:datetime',
            [
                'attribute' => 'created_by',
                'value' => 'creatore.username',
            ],
            'updated_at:datetime',
            [
                'attribute' => 'updated_by',
                'value' => 'modificatore.username',
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
        'toolbar' => [
            [
                'content'=>
                    Html::a('<i class="glyphicon glyphicon-plus"></i> Aggiungi nuovo', ['create'], ['class' => 'btn btn-primary']) . ' '.
                    Html::a('<i class="glyphicon glyphicon-repeat"></i> Ripristina visualizzazione', ['index'], ['class' => 'btn btn-info']),
            ],
            '{export}',
            '{toggleData}',
        ],

    ]);

I change with your solution in:

        [
            'attribute' => 'status',
            'value' => function ($model) {
                var_dump($model->ipass->status); die();
                return !empty($model->ipass->status) ? 'Not Available' : 'Available';
            },

        ],

And return error:
Trying to get property ‘status’ of non-object

And If I insert in index.php:

<?php
$model->ipass->status == Model::STATUS_NOT_AVAILABLE ? 'Not Available': 'Available';
?>

return error:
Undefined variable: model

Show me pls var_dump of:

'value' => function ($model) {
     var_dump($model, $model->ipass); die();
              
 },

Do you have a relation with a name of “ipass” in $model?

Show me pls class of $model object

Remove keyword ‘static’ before function …

[ 
'attribute' => 'status',
 'value' => function ($model) {
 return empty($model->ipass->status) ? 'Non disponibile' : 'Disponibile'; 
 }, 
],

Modify code like below:

[
  'attribute' => 'status',
  'value' => function ($model) {
       return $model->ipass && empty($model->ipass->status) ? 'Non disponibile' : 'Disponibile';
  },
],

I have set this in model Numeri:

/**
 * Gets query for [[Ipass]].
 *
 * @return \yii\db\ActiveQuery|GestionalepcQuery
 */
public function getElencoip()
{
    return $this->hasOne(Gestionalepc::className(), ['elencoip' => 'ipass']);
}

and I insert const:

class Numeri extends \yii\db\ActiveRecord
{
    /**
     * @var string
     */
    const STATUS_NOT_AVAILABLE = 'Non Disponibile';
    const STATUS_AVAILABLE = 'Disponibile';

public function rules()
{
    return [
        [['ipass','status', 'created_by', 'updated_by'], 'integer'],
        [['created_at', 'updated_at', 'status'], 'safe'],
        [['elenconumeri'], 'string', 'max' => 80],
        [['elenconumeri'], 'unique'],
    ];
}

I certainly can’t associate a string with a const

I modified in views/numeri/index.php

    [
        'attribute' => 'status',
        'value' => function ($model) {
            return $model->ipass && empty($model->ipass->status) ? 'Non disponibile' : 'Disponibile';
        },
    ],

but the error is
Undefined variable: model