25

How can I remove or replace strings (not set) in my GridView and ListView?

6 Answers 6

79

Two ways that I know (now):

Formatter

Set nullDisplay of Formatter to something other than null. You can do this in global configuration or for the single GridView or DetailView.

Globally (typically in config/web.php or <application>/config/main.php files):

'components' => [
    ...
    'formatter' => [
        'class' => 'yii\i18n\Formatter',
        'nullDisplay' => '',
    ],
    ...
],

In certain GridView (same with DetailView):

<?= GridView::widget([
    'dataProvider' => $myProvider,
    'formatter' => ['class' => 'yii\i18n\Formatter','nullDisplay' => ''],
    'columns'      => [
        ...
    ],
]); ?>

Set the value

Probably not so elegant. In a certain GridView:

<?= GridView::widget([
    'dataProvider' => $myProvider,
    'columns'      => [
        ...
        [
            'attribute' => 'some_attribute',
            'format'    => 'raw',
            'value'     => function (ModelClass $model) {
                if ($model->some_attribute != null) {
                    return $model->some_attribute; 
              //or: return Html::encode($model->some_attribute)
                } else {
                    return '';
                }
            },
        ],
        ...
    ],
]); ?>

Or in a certain DetailView:

<?= DetailView::widget([
    'model'      => $model,
    'attributes' => [
        ...
        [
            'attribute' => 'some_attribute',
            'value' => $model->some_attribute != null ? $model->some_attribute : '', 
      //or: 'value' => $model->some_attribute != null ? Html::encode($model->some_attribute) : '',
        ],
        ...
    ],
]) ?>

Two hints

If several approaches are used at the same time: setting the value (directly or by function) overrides the formatter configuration of the Grid/DetailView, and this in turn overrides a global formatter configuration.

You can also define something different than an empty string. E.g. if bootstrap is used you may want to use \yii\bootstrap\Html::icon('question-sign') (or '<span class="glyphicon glyphicon-question-sign"></span>') to get a symbol for missing values.

1
  • Ok but consider using $attribute !== null or similar instead of != null.
    – Alexeyer
    Oct 20, 2022 at 19:55
10

use this:

use Yii;

...

Yii::$app->formatter->nullDisplay = 'N\A';
0
1

for kartik\grid\GridView;

'class' => 'kartik\grid\EditableColumn',
'attribute'=>'myAttribute',
'header' => 'myHeader',                                            
'editableOptions' => [                         
    'inputType' => \kartik\editable\Editable::INPUT_TEXT,
    'valueIfNull' => '-',

/**
 * @var string the value to be displayed. If not set, this will default to the attribute value. If the attribute
 * value is null, then this will display the value as set in [[valueIfNull]].
 */
public $displayValue;
1

Set emptycell in gridview config:

<?= GridView::widget([
  'dataProvider' => $dataProvider,
  'filterModel' => $searchModel,
  'emptyCell'=>'-',
  'columns' => [
      ['class' => 'yii\grid\SerialColumn'],
.........
      ['class' => 'yii\grid\ActionColumn'],
  ],
 ]); ?>

or in :

  'attribute' => 'description',
  'label' => Yii::t('app', 'description'),
  'value' => function($data) {
         return !empty($data->description) ? $data->description : '-';
   }
1
  • This is not the right. As documentation says emptyCell is to show when nothing is returned.
    – Prescol
    Oct 12, 2017 at 0:31
0

For DetailView and GridView I use if else in columns

DetailView:

<?= DetailView::widget([
   'model' => $model,
   'attributes' => [
      ['label' => 'labelName', 'value' => function($data) {
         if (!empty($data->tDeal->DealDate)) { 
            return $data->tDeal->DealDate; 
         } else { return ''; }
      }],
    ],
])
?>

GridView:

<?= GridView::widget([
   'dataProvider' => $dataProvider,
   'columns' => [
      ['label' => 'labelName', 'value' => function($data) {
         if (!empty($data->tDeal->DealDate)) { 
            return $data->tDeal->DealDate; 
         } else { return ''; }
      }],
    ],
])
?>
-3

I wouldnt recommend set nullDisplay method. It is best practical to check if the variable you access is null or not.

I would do

//model code
public function getProjectName()
{
    $project = $this->project;
    return ($project) ? $project->name : '';
}

//your gridview
<?= GridView::widget([
'dataProvider' => $myProvider,
'columns'      => [
    ...
    [
        'attribute' => 'some_attribute',
        'format'    => 'raw',
        'value'     => function (ModelClass $model) {
             $model->projectName;
        },
    ],
    ...
],
 ]); ?>
1
  • 3
    Why is checking each item to null is better than setting formatter to null. Explain
    – Double H
    Aug 13, 2015 at 16:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.