Skip to content

Instantly share code, notes, and snippets.

@liruqi
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liruqi/b1ef5a03eff97aae57c7 to your computer and use it in GitHub Desktop.
Save liruqi/b1ef5a03eff97aae57c7 to your computer and use it in GitHub Desktop.
Yii2 Drop list down value mapping
<?php
# Drop down list value mapping
# File models/Report.php
public function areaTypeMapping() {
return ['公共区域', '办公区域'];
}
# views/report/_form.php
$form->field($model, 'areatype')->dropDownList($model->areaTypeMapping());
# views/report/index.php
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $isAdmin ? $searchModel : null,
'columns' => [
'id',
'phone',
//'areatype',
[
'attribute' => 'areatype',
'label' => '类型',
'content' => function($model, $key, $index, $column) {
$m = $model->areaTypeMapping();
return $m[$model->areatype];
},
],
'area',
'content',
// 'image',
['class' => 'yii\grid\ActionColumn'],
],
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment