0

Could not find that in documentation and around google as well.

I render table's widget like this:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'last_name',
        'available',

        ['class' => 'yii\grid\ActionColumn'],
    ],

And that renders a table like this:

...
<tr>
   <td>Some last name</td>
   <td>1</td>
</tr>
..

As you might already guess, available represents a boolean. It can be either 0 or 1. But I don't want to display it like this. I want 1 to become 'Yes' and 0 to become 'No'.

So how would I do str_replace(array('1', '0'), array('Yes', 'No')) on that column??

Yang
  • 8,580
  • 8
  • 33
  • 58

4 Answers4

2

No need to write switches, str_replace or other complicated things.

The most laconic way will be:

'available:boolean',

Which is equivalent to:

[
    'attribute' => 'available',
    'format' => 'boolean',
],

Use the latter if you want more detailed configuration of a column.

The main advantage of this method is that formatter will be used to display boolean value, and displayed text will depend on source language that is set in application config. So it's basically internationalization for you.

Learn framework possibilities before writing something like str_replace.

Official docs:

P.S. And by the way this is already covered in this question.

Community
  • 1
  • 1
arogachev
  • 33,150
  • 7
  • 114
  • 117
1

According to the documentation for the DataColumn, you can set an anonymous function to a column. This way, you can format the column data, or do whatever fits you. Example:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        'last_name',
        [
            'attribute' => 'available',
            'value' => function($data){return $data->available == true ? 'Yes': 'No';},
        ]

        ['class' => 'yii\grid\ActionColumn'],
    ],

Please note that the $data variable is the model for one item from the $dataProvider, so you can access whatever you want from that model.

XzAeRo
  • 566
  • 5
  • 11
0

You can use switch function

switch($value){
   case '1':
     $value = 'Yes';
     break;
   case '0':
     $value = 'No';
     break;
   default:
     $value = $value;
 ;

This should handle your problem.

0

You can use a function

         [
            'attribute' => 'attivo',
            'label' => 'Attivo',
            'value' => function($model, $index, $dataColumn) { 
                        switch ($model->attivo) {
                            case '0':
                               return 'Not';
                                break;
                            case '1':
                                return 'Yes';
                                break;
                        }
                     },
            'contentOptions' => ['style' => 'width:90px;'], 
            'filter' => [ '1' => 'Yes', '0'=>'Not'],
        ],
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107