Yii2:删除GridView和DetailView中的“(未设置)”以获取空值

时间:2015-05-28 14:03:06

标签: gridview view yii2 detailview

如何在GridView和ListView中删除或替换字符串(not set)

6 个答案:

答案 0 :(得分:56)

我知道的两种方式(现在):

<强>格式化

nullDisplay of Formatter设置为null以外的值。您可以在全局配置中执行此操作,也可以在单个GridView或DetailView中执行此操作。

全球(通常在config/web.php<application>/config/main.php个文件中):

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

在某些GridView中(与DetailView相同):

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

设置值

可能不那么优雅。在某个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 '';
                }
            },
        ],
        ...
    ],
]); ?>

或在某个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) : '',
        ],
        ...
    ],
]) ?>

两个提示

如果同时使用多种方法:设置值(直接或通过函数)会覆盖Grid / DetailView的格式化程序配置,这反过来会覆盖全局格式化程序配置。

您还可以定义与空字符串不同的内容。例如。如果使用了bootstrap,您可能需要使用\yii\bootstrap\Html::icon('question-sign')(或'<span class="glyphicon glyphicon-question-sign"></span>')来获取缺失值的符号。

答案 1 :(得分:1)

在gridview配置中设置emptycell:

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

或在:

  'attribute' => 'description',
  'label' => Yii::t('app', 'description'),
  'value' => function($data) {
         return !empty($data->description) ? $data->description : '-';
   }

答案 2 :(得分:0)

代表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;

答案 3 :(得分:0)

使用此:

use Yii;

...

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

答案 4 :(得分:0)

对于DetailView和GridView,我在列中使用else

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 ''; }
      }],
    ],
])
?>

答案 5 :(得分:-2)

我不建议设置nullDisplay方法。最好检查您访问的变量是否为空。

我愿意

//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;
        },
    ],
    ...
],
 ]); ?>