使用 GridView 在 Yii2.0 中显示过滤器的空白行 [英] Showing blank rows for filters in Yii2.0 with GridView
问题描述
我已经在 Yii2.0 中设置了 GridView 来创建我的表,如下所示:
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.
这是为什么?我错过了什么?
PS:搜索模型本身工作正常,这意味着,当我添加到 url ?title=asd
时,它实际上得到了搜索结果!
PS: The search model itself works fine, meaning, when I add to the url ?title=asd
it actually get the search results!
推荐答案
根据 $filterModel
属性的文档:
According to the documentation of the $filterModel
property:
请注意,为了显示用于过滤的输入字段,列必须设置其 yii\grid\DataColumn::$attribute
属性或设置 yii\grid\DataColumn::$filter
设置为输入字段的 HTML 代码.
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.
所以你需要在你的列上设置 yii\grid\DataColumn::$attribute
属性,并且在大多数情况下这使得 value
变得不必要:
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' ],
],
]);
?>
这篇关于使用 GridView 在 Yii2.0 中显示过滤器的空白行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!