2014-12-05 12 views
7

mam skonfigurować GridView do crfeate moim stole Yii2.0 następująco:pokazując puste wiersze dla filtrów w Yii2.0 z 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' ], 
    ], 
]); 
?> 

uzyskać wszystkie poprawne dane, ale zamiast wejść filtrów, Otrzymuję puste wiersze.

enter image description here

Dlaczego tak jest? czego mi brakuje?

PS: Sam model wyszukiwania działa poprawnie, co oznacza, że ​​po dodaniu adresu URL do adresu URL ?title=asd faktycznie uzyskuje on wyniki wyszukiwania!

Odpowiedz

4

Według nieruchomości documentation of the $filterModel:

Należy zauważyć, że w celu wykazania pole wejściowe do filtrowania, kolumna musi mieć swój zestaw yii\grid\DataColumn::$attribute własność lub mieć yii\grid\DataColumn::$filter ustawiony jako kod HTML dla pola wejściowego.

Więc trzeba ustawić yii\grid\DataColumn::$attribute nieruchomości na kolumnach i w większości przypadków to sprawia, że ​​value niepotrzebna:

<?= \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

Dziękuję bardzo. Nie mam pojęcia, jak to przegapiłem. – Peon

1

Innym możliwym powodem pustej linii (nie w plakatów dokładny przypadek)

Brakująca/niepoprawna deklaracja public function rules() w modelu wyszukiwania. W Yii 1 można łączyć łańcuch, w Yii2 muszą to być rzeczywiste elementy tablicy.

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

A jeśli używasz właściwości filterModel, używany tam model musi rozciągać się na yii \ base \ Model. – dataskills