Yii2 gridview过滤来自多个值的列表(不是下拉列表过滤器)

时间:2018-03-02 02:21:53

标签: gridview yii2 yii2-basic-app

我的gridview数据

    day          modeler    total  
    2018-1-05     ABC        5                         
    2018-1-06     DEF        8        
    2018-1-06     CAB        3   
    2018-1-06     GHI        3   
    2018-1-06     KLM        3   

我有这样的网格视图。现在,只能逐个过滤建模器。我可以使用多行搜索框并只粘贴“ABC DEF CAB”,它会过滤下面的3个结果吗?

day          modeler    total  
2018-1-05     ABC        5                         
2018-1-06     DEF        8        
2018-1-06     CAB        3

我的控制器

public function actionIndex()
{
    $searchModel = new ModelerSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

我的搜索模型

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

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

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

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
        ]);

        $query->andFilterWhere(['like', 'modeler', $this->modeler])
            ->andFilterWhere(['like', 'total', $this->total]);

        return $dataProvider;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要通过gridview中的modeler字段进行搜索,并希望其工作方式如果您输入"ABC",则应显示匹配的记录,如果输入{{1} }如果有空格,它应该显示2条与其中任何一条匹配的记录。

所以,首先,在搜索模型的基础上添加

"ABC DEF"

然后将搜索功能更新为以下

private $_selections = [];

现在转到您的网格视图和过滤器输入public function search( $params ) { $query = Modeler::find (); // add conditions that should always apply here $dataProvider = new ActiveDataProvider ( [ 'query' => $query , ] ); $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; } if ( $this->modeler !== null && $this->modeler !== '' ) { $this->_selections = preg_split ( '/\s+/i' , $this->modeler ); $query->andFilterWhere ( [ 'IN' , 'modeler' , $this->_selections ] ); } // grid filtering conditions $query->andFilterWhere ( [ 'id' => $this->id , ] ); $query->andFilterWhere ( [ 'like' , 'total' , $this->total ] ); return $dataProvider; } 中的modeler列类型,并观察它。