在yii2 gridview中搜索不起作用

时间:2022-10-05 08:46:41

I installed yii2 export menu.it working fine but search is not working properly.i cant understand whats going wrong? please help to fix it... here is code-

我安装了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 个解决方案

#1


0  

Add to following function to your search model

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

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;
    }

#2


0  

In companiesController.php , $dataProvider = $searchModel->search(Yii::$app->request->queryParams); this line was commented by mistake.

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

Thanks all for help and response..

谢谢大家的帮助和回应..

#1


0  

Add to following function to your search model

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

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;
    }

#2


0  

In companiesController.php , $dataProvider = $searchModel->search(Yii::$app->request->queryParams); this line was commented by mistake.

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

Thanks all for help and response..

谢谢大家的帮助和回应..