我正在尝试将过滤器添加到_form.php
中包含的GridView小部件中。网格显示正常,显示偶数过滤器字段,但过滤器不工作。
这是我的代码:
<?php
$searchModel = New CitySearch(); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
答案 0 :(得分:4)
我找到了解决方案。在将搜索模型附加到GridView之前,必须先搜索搜索模型。因此,我只需添加一行即可使其工作:
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
整个代码都是这样的:
<?php
$searchModel = New CitySearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'city_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>