我已经设置了GridView来在Yii2.0中伪造我的表,如下所示:
<?= \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' ],
],
]);
?>
我得到了所有正确的数据,但我没有过滤输入,而是得到空行。
为什么?我错过了什么?
PS:搜索模型本身运行正常,这意味着,当我添加到网址?title=asd
时,它实际上会获得搜索结果!
答案 0 :(得分:4)
根据documentation of the $filterModel
属性:
请注意,为了显示用于过滤的输入字段,列必须设置其
yii\grid\DataColumn::$attribute
属性或将yii\grid\DataColumn::$filter
设置为输入字段的HTML代码。
因此,您需要在列上设置yii\grid\DataColumn::$attribute
属性,在大多数情况下,这会使value
不必要:
<?= \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 :(得分:1)
空白行的另一个可能原因:(不是海报确切的情况)
搜索模型中public function rules()
的声明丢失/不正确。在Yii 1中你可以连接字符串,在Yii2中它们需要是实际的数组元素。
return [
[['authorId, title, publishFrom'], 'safe'], //WRONG
[['authorId', 'title', 'publishFrom'], 'safe'], //CORRECT
];