在我的_form.php中,我创建了一个类别的依赖下拉列表。在index.php中的GridView中选择类别和子类别后,它会显示类别和子类别的ID。如何显示category_name和sub_category名称而不是ID
<?= $form->field($model, 'category_id')->dropDownlist(
ArrayHelper::map(Category::find()->all(), 'id', 'category_name'),
[
'prompt' => 'Select Category',
'onchange' =>'
$.post( "index.php?r=sub-cat/lists&id='.'"+$(this).val(), function( data ) {
$( "select#tickets-sub_cat_id" ).html( data );
});'
]
); ?>
<?= $form->field($model, 'sub_cat_id')->dropDownlist(
ArrayHelper::map(SubCat::find()->all(), 'id', 'sub_category'),
[
'prompt' => 'Select Sub Category',
]
); ?>
GridView代码:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'rowOptions' => function($model) {
if($model->status == 'On Going')
{
return ['class' => 'danger'];
} else if($model->status == 'Done') {
return ['class' => 'success'];
}
},
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'category_id',
'sub_cat_id',
// 'request_title',
'status',
[
'attribute' => 'time_start',
'value' => 'time_start',
'filter' => DatePicker::widget([
'model' => $searchModel,
'attribute' => 'time_start',
'pluginOptions' => [
'autoclose' => true,
'format' => 'yyyy-m-dd',
'todayHighlight' => true
]
]),
],
'time_end',
// 'details',
// 'room_no',
// 'employee_id',
'room_no',
['class' => 'yii\grid\ActionColumn'],
我的模特
public function getCategory0()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
public function getSubCat()
{
return $this->hasOne(SubCat::className(), ['id' => 'sub_cat_id']);
}
答案 0 :(得分:1)
假设您的模型具有正确的关系(例如&#34; getCategory&#34;),您可以将网格列定义为
'category.category_name'