2015-05-28 13 views

Odpowiedz

43

Dwa sposoby, które znam (teraz):

formater

Set nullDisplay of Formatter do czegoś innego niż null. Możesz to zrobić w konfiguracji globalnej lub w pojedynczym widoku GridView lub DetailView.

Globalnie (zazwyczaj w config/web.php lub <application>/config/main.php pliki):

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

W pewnym GridView (tak samo z DetailView):

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

ustawiona wartość

Prawdopodobnie nie tak elegancki . W pewnym 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 ''; 
       } 
      }, 
     ], 
     ... 
    ], 
]); ?> 

lub w określonej 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) : '', 
     ], 
     ... 
    ], 
]) ?> 

Dwie podpowiedzi

Jeśli kilka podejść są używane w tym samym czasie: ustawienie wartości (bezpośrednio lub przez funkcja) zastępuje konfigurację formantu w Grid/DetailView, a to z kolei zastępuje konfigurację formantu globalnego.

Można również zdefiniować coś innego niż pusty ciąg. Na przykład. jeśli używasz bootstrap, możesz użyć '<span class="glyphicon glyphicon-question-sign"></span>', aby uzyskać symbol brakujących wartości.

-2

Nie polecam zestawu metody nullDisplay. Najlepiej jest sprawdzić, czy zmienna, do której uzyskujesz dostęp, jest pusta lub nie.

zrobiłbym

//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

Dlaczego sprawdzenie każdej pozycji do wartości null jest lepsze niż ustawienie formtera na wartość null. Wyjaśnij –

1

zestaw emptycell w gridveiw config:

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

lub w:

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

To nie jest prawo. Dokumentacja mówi, że emptyCell ma pokazywać, kiedy nic nie jest zwracane. – Prescol

0

dla 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;