Yang
Yang

Reputation: 8711

How to get value inside GridView?

I have a timestamp column, and I simply want to format it. In GridView I have the following:

[
    'attribute' => 'timestamp',
    'filter' => false,
    'value' => function($model, $key, $index, $column) {
         // How to get current timestamp value here???
    }
],

Documentation says, $model and $column both return objects, but I still could not find methods that provide column's data. How would I go about this?

Upvotes: 0

Views: 346

Answers (2)

arogachev
arogachev

Reputation: 33548

You can call any attribute through getter in closure using $model and that will return attribute for current model (according to row in GridView):

[
    'attribute' => 'timestamp',
    'filter' => false,
    'value' => function($model, $key, $index, $column){
        return $model->timestamp;
    }
],

Obviously such return doesn't make any sense, but you can format it somehow you want. There are some built-in options for date / datetime formatting in Yii2, you can check them in official docs here:

Upvotes: 3

The Humble Rat
The Humble Rat

Reputation: 4696

I use the following with Yii 1:

 [
        'attribute' => 'timestamp',
        'filter' => false,
        'value' => 'Custom::formatDateTime($data->date_stamp)',
    ],

Obviously formatDateTime is a custom method I created.

Upvotes: 0

Related Questions