在yii2 gridview中搜索不起作用

时间:2017-09-12 13:01:54

标签: gridview

我安装了yii2导出menu.it工作正常,但搜索工作不正常。我无法理解什么是错误的?请帮忙解决它... 这是代码 -

<?php
        $gridColumns = [
        [
        'class' => 'yii\grid\SerialColumn',
        ],
        'name',
        'company_mail',    
        'created',
         'modified',
        'modified_by_id',
        ['class' => 'yii\grid\ActionColumn', 'urlCreator'=>function(){return '#';}],

    ]; ?>

<?php
echo ExportMenu::widget([
'dataProvider' => $dataProvider,
'filterModel'=>$searchModel,
'columns' => $gridColumns,
'target' => ExportMenu::TARGET_BLANK,
  ]); 

?>
<?php 
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]); 
?>

2 个答案:

答案 0 :(得分:0)

将以下功能添加到搜索模型

public function search($params)
    {
        $query = YourModel::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [

                ]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }


        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'company_mail', $this->company_mail])
            ->andFilterWhere(['like', 'modified', $this->modified])
            ->andFilterWhere(['like', 'created', $this->created])
            ->andFilterWhere(['like', 'modified_by_id', $this->modified_by_id]);

        return $dataProvider;
    }

答案 1 :(得分:0)

在companiesController.php中,   $ dataProvider = $ searchModel-&gt; search(Yii :: $ app-&gt; request-&gt; queryParams); 这条线被误判了。

感谢所有人的帮助和回应..