Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing blank rows for filters in Yii2.0 with GridView

Tags:

php

yii

I have set up GridView to crfeate my table in Yii2.0 as follows:

<?= \yii\grid\GridView::widget([
    'dataProvider' => $model->dataProvider,
    'filterModel' => $model->searchModel,
    'columns' => [
        [
            'label' => Yii::t( $cat, 'Id' ),
            'value' => 'id',
        ],
        [
            'label' => Yii::t( $cat, 'Title' ),
            'format' => 'raw',
            'value' => function ( $data ) {
                if ( $data['status_code'] != 5 )
                {
                    return Html::a( $data['title'], '/signer/view/' . $data['id'] );
                }
                else
                {
                    return $data['title'];
                }
            },
        ],
        [
            'label' => Yii::t( $cat, 'Description' ),
            'value' => 'description',
        ],
        [
            'label' => Yii::t( $cat, 'Filename' ),
            'value' => 'filename',
        ],
        [
            'label' => Yii::t( $cat, 'Status' ),
            'value' => 'status',
            'contentOptions' => function ( $data ) {
                    $statuses = [
                        1 => 'text-primary',    # New
                        2 => 'text-warning',    # Unsigned
                        3 => 'text-warning',    # Partially signed
                        4 => 'text-success',    # Signed
                        5 => 'text-danger',     # Deleted
                    ];
                    return [ 'class' => $statuses[$data['status_code']] ];
                }
        ],
        [
            'label' => Yii::t( $cat, 'Created' ),
            'value' => 'created',
        ],
        //[ 'class' => 'yii\grid\ActionColumn' ],
    ],
]);
?>

I get all the correct data, but instead of filter inputs, I get empty rows.

enter image description here

Why is that? What am I missing?

PS: The search model itself works fine, meaning, when I add to the url ?title=asd it actually get the search results!

like image 651
Peon Avatar asked Dec 05 '14 11:12

Peon


1 Answers

According to the documentation of the $filterModel property:

Note that in order to show an input field for filtering, a column must have its yii\grid\DataColumn::$attribute property set or have yii\grid\DataColumn::$filter set as the HTML code for the input field.

So you need to set yii\grid\DataColumn::$attribute property on your columns and in most of the cases this makes the value unnecessary:

<?= \yii\grid\GridView::widget([
    'dataProvider' => $model->dataProvider,
    'filterModel' => $model->searchModel,
    'columns' => [
        [
            'label' => Yii::t( $cat, 'Id' ),
            'attribute' => 'id',
        ],
        [
            'label' => Yii::t( $cat, 'Title' ),
            'format' => 'raw',
            'attribute' => 'title',
            'value' => function ( $data ) {
                if ( $data['status_code'] != 5 )
                {
                    return Html::a( $data['title'], '/signer/view/' . $data['id'] );
                }
                else
                {
                    return $data['title'];
                }
            },
        ],
        [
            'label' => Yii::t( $cat, 'Description' ),
            'attribute' => 'description',
        ],
        [
            'label' => Yii::t( $cat, 'Filename' ),
            'attribute' => 'filename',
        ],
        [
            'label' => Yii::t( $cat, 'Status' ),
            'attribute' => 'status',
            'contentOptions' => function ( $data ) {
                    $statuses = [
                        1 => 'text-primary',    # New
                        2 => 'text-warning',    # Unsigned
                        3 => 'text-warning',    # Partially signed
                        4 => 'text-success',    # Signed
                        5 => 'text-danger',     # Deleted
                    ];
                    return [ 'class' => $statuses[$data['status_code']] ];
                }
        ],
        [
            'label' => Yii::t( $cat, 'Created' ),
            'attribute' => 'created',
        ],
        //[ 'class' => 'yii\grid\ActionColumn' ],
    ],
]);
?>
like image 80
nemesv Avatar answered Oct 06 '22 17:10

nemesv