wilsonim
(Wilson Im)
1
Hi All,
In the index file of Model User,
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'username',
[
'attribute' => 'role_id',
'value' => 'role.role_name',
],
'Note:ntext',
'email:email',
'status',
'created_at',
'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
how to change field status to ACTIVE or INACTIVE instead of number 0 or 10.
Thanks.
Regards,
Wilson
patrickm
(Yiiframework)
2
softark
(Softark)
3
I would usually create a getter method that returns the string representation of the status field.
public function getStatusText()
{
switch ($this->status) {
case self::ACTIVE
$text = "Active";
break;
case self::INACTIVE:
$text = "Inactive";
break;
default:
$text = "(Undefined)";
break;
}
return $text;
}
And you can use it in GridView’s column.
[
'attribute' => 'status',
'value' => 'statusText',
],
wilsonim
(Wilson Im)
4
Thanks softark. This method works.
wilsonim
(Wilson Im)
5
Hi,
in DetailView Widget how to map these matter too? because i try same solution does not work.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'username',
'email:email',
[
'attribute' => 'status',
'value' => 'StatusText',
],
'created_at',
'updated_at',
'role.role_name',
'note:ntext',
],
]) ?>
patrickm
(Yiiframework)
6
Try "statusText" with a lowercase "s".
wilsonim
(Wilson Im)
7
Hi Patrick Jones,
in model user function i use Big letter getStatusText() so i called it with StatusText. While in index file, it works with StatusText.
wilsonim
(Wilson Im)
8
I solve with this method
[
'attribute' => 'status',
'value' => $model->status == 10 ? 'ACTIVE' : 'INACTIVE',
],