2017-11-25 2 views
0

내 _form.php에서 카테고리에 대한 종속 드롭 다운을 만들었습니다. index.php의 GridView에서 카테고리와 하위 카테고리를 선택하면 카테고리와 하위 카테고리의 ID가 표시됩니다. "예 (모델이 적절한 관계를 가지고 있다고 가정어떻게 ID 대신 이름을 표시합니까 - GridView Yii2

<?= 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']); 
} 

답변

1

: 어떻게 대신 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 코드의 범주 이름과 sub_category 이름을 표시 할 getCategory "), 격자 열을

'category.category_name' 
로 정의 할 수 있습니다
+0

내 getCategory 및 subCategory에 "return $ this-hasOne"이 있습니다. – Smurfzzz

+0

이미 시도했지만 그 중 하나가 gridview "Not Set"에 표시됩니다. – Smurfzzz

+0

문제가 데이터에 있음을 나타내며 관계가 저장되지 않음을 나타냅니다. – pat