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.
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!
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 haveyii\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' ],
],
]);
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With