7

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!

0

2 Answers 2

4
+200

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' ],
    ],
]);
?>
1
  • Thank you a lot. No idea how I missed that.
    – Peon
    Commented Dec 18, 2014 at 12:23
1

Another possible reason for the blank line: (not in posters exact case)

Missing/incorrect declaration of the public function rules() in the search model. In Yii 1 you could concatenate the string, in Yii2 they need to be actual array elements.

return [
    [['authorId, title, publishFrom'], 'safe'],       //WRONG
    [['authorId', 'title', 'publishFrom'], 'safe'],   //CORRECT
];
1
  • And if you are using the filterModel property the model used there must extend yii\base\Model.
    – dataskills
    Commented Apr 6, 2016 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.