2014-12-05 58 views
7

過濾器的空行我已經設置了GridView控件crfeate我的表中Yii2.0如下:顯示在Yii2.0與GridView控件

<?= \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' ], 
    ], 
]); 
?> 

我得到的所有正確的數據,而是過濾器的輸入,我得到空行。

enter image description here

這是爲什麼?我錯過了什麼?

PS:搜索模式本身工作正常,這意味着,當我添加到網址?title=asd它實際上得到的搜索結果!

回答

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' ], 
    ], 
]); 
?> 
+0

非常感謝。不知道我是如何錯過的。 – Peon 2014-12-18 12:23:56

1

另一個可能的原因是空白行:(而不是在海報確切的情況下)

在搜索模型中缺少/不正確聲明public function rules()。在Yii 1中,你可以連接字符串,在Yii2中它們需要是實際的數組元素。

return [ 
    [['authorId, title, publishFrom'], 'safe'],  //WRONG 
    [['authorId', 'title', 'publishFrom'], 'safe'], //CORRECT 
]; 
+0

如果您使用filterModel屬性,那裏使用的模型必須擴展yii \ base \ Model。 – dataskills 2016-04-06 16:40:12