У модели Users есть поле в БД sex (пол)
значениря в БД записаны male и female
как в GridView вывести значения "муж" и "жен"?
Заменить value у атрибута в GridView
Re: Заменить value у атрибута в GridView
Сделал таким образом, но кажется как-то не так это делается:
в самой модели:
Код: Выделить всё
[
'attribute' => 'sex',
'value' => function($data){
return \backend\models\Users::$sex_ru[$data->sex];
},
'format' => 'text'
],
Код: Выделить всё
/**
* @var array
*/
static $sex_ru = [
'female' => 'жен',
'male' => 'муж'
];
Re: Заменить value у атрибута в GridView
Это именно так и делается. Хотя, можно немного красивее (пример):
Код: Выделить всё
// model
/**
* @param bool $colored
* @return array
*/
public static function getStatusLabels($colored = false) {
$statuses = [
self::STATUS_ACTIVE => ($colored ? '<span style="color: #60b35a">Активно</span>' : 'Активно'),
self::STATUS_DISABLED => ($colored ? '<span style="color: #687269">Отложено</span>' : 'Отложено'),
];
if(can('moder')) {
$statuses[self::STATUS_DELETED] = ($colored ? '<span style="color: rgb(200, 217, 204)">Удалён</span>' : 'Удалён');
}
return $statuses;
}
/**
* @param bool $colored
* @return mixed|string
*/
public function getStatusLabel($colored = false) {
$labels = self::getStatusLabels($colored);
return isset($labels[$this->status]) ? $labels[$this->status] : 'Неизвестный статус';
}
// view
[
'attribute' => 'status',
'value' => function ($model) {
/** @var $model Product */
return $model->getStatusLabel(true);
},
'format' => 'raw',
'filter' => Product::getStatusLabels()
],