简体   繁体   English

如何显示名称而不是ID-GridView Yii2

[英]How do I display the name instead of ID - GridView Yii2

In my _form.php I have created a dependent dropdown for category. 在我的_form.php中,我为类别创建了一个依赖下拉列表。 After selecting a category and a sub category in my GridView in index.php it displays the ID of the category and subcategor. 在index.php的GridView中选择一个类别和一个子类别后,它将显示该类别和子类别的ID。 How do I display the category_name and sub_category name instead of the 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 code: 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'],

My model 我的模特

public function getCategory0()
{
    return $this->hasOne(Category::className(), ['id' => 'category_id']);
}

public function getSubCat()
{
    return $this->hasOne(SubCat::className(), ['id' => 'sub_cat_id']);
}

假设您的模型具有适当的关系(例如“ getCategory”),则可以将网格列定义为

'category.category_name'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM