简体   繁体   English

如何在 yii2 中过滤 gridview 表?

[英]How to filter a gridview table in yii2?

I have two gridview tables currently, one shows all the data and the other i want it to show only data where id = 2. Is it possible to filter only one table without affecting the other?我目前有两个 gridview 表,一个显示所有数据,另一个我希望它只显示 id = 2 的数据。是否可以只过滤一个表而不影响另一个? I know I can filter from the search model but that will affect all tables and i want it to affect only one.我知道我可以从搜索模型中过滤,但这会影响所有表,我希望它只影响一个。

Can i have two dataprovider?我可以有两个数据提供者吗?

This is the code in my search model:这是我的搜索模型中的代码:

class JobPlanningSearch extends JobPlanning
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'priority', 'employer_id', 'client_id', 'status', 'activity'], 'integer'],
            [['job_description', 'impediment', 'date', 'estimated_time', 'due_date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = JobPlanning::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,
            'priority' => $this->priority,
            'client_id' => $this->client_id,
            'employer_id' => $this->employer_id,
            'estimated_time' => $this->estimated_time,
            'status' => $this->status,
            'activity' => $this->activity,
            //'actual' => $this->actual,
            //'actual' => 1,
        ]);

        $query->andFilterWhere(['like', 'job_description', $this->job_description]);
        $query->andFilterWhere(['like', 'activity', $this->activity]);

        return $dataProvider;
    }

You need two dataproviders, like this:您需要两个数据提供者,如下所示:

$searchModelOne = new JobPlanningSearch();        
$dataProviderOne = $searchModelOne->search(Yii::$app->request->queryParams);
$dataProviderOne->pagination->pageParam = 'dp-one-page'; //set page param for first dataprovider
$searchModelTwo = new JobPlanningSearch();   
searchModelTwo->id = 2;     // set id = 2 in second dataprovider
$dataProviderTwo = $searchModelTwo->search(Yii::$app->request->queryParams);
$dataProviderTwo->pagination->pageParam = 'dp-two-page'; //set page param for second dataprovider

You will have to manipulate the DataProvider in the controller.您必须在控制器中操作 DataProvider。

$searchModel = new JobPlanningSearch();        
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->query->andWhere(['=','id',2]);
  1. remove it from the search function in JobPlanningSearch.从 JobPlanningSearch 的搜索功能中删除它。

    $query->andFilterWhere([ $query->andFilterWhere([

     /* 'id' => $this->id, */ // you must remove it from here 'priority' => $this->priority, 'client_id' => $this->client_id, 'employer_id' => $this->employer_id, 'estimated_time' => $this->estimated_time, ...

    ]); ]);

In this case, the ID will always be 2.在这种情况下,ID 将始终为 2。

But if you want to have a default value (the first time) and allow the user to change it, you should skip step 2 .但是如果你想有一个默认值(第一次)并允许用户改变它,你应该跳过第 2 步 And add a condition in the controller:并在控制器中添加一个条件:

...
if(count(Yii::$app->request->queryParams) == 0){
    $dataProvider->query->andWhere(['=','id',2]);
}

This is just an example, you should check that the ID field has not been sent in the queryparams.这只是一个示例,您应该检查查询参数中是否未发送 ID 字段。

You need to use two searchModels, one for each GridView and of course two data providers.您需要使用两个 searchModel,每个 GridView 一个,当然还有两个数据提供者。

Also you need to change the formName attribute of one of the searchModels to avoid filtering to affects both grids.您还需要更改其中一个formName属性,以避免过滤影响两个网格。

And the in the controller, when passing parameters to the search() method of the modified searchModel, pass the params in the new name you assigned to formName in this searchModel.而在控制器中,当向修改后的searchModel的search()方法传递参数时,将params传入你在这个searchModel中分配给formName的新名称中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM