Yii2 grid filter on matching column

I have joined two tables in my model finder with

$query = Surveys::find()->select('surveys.*,regions.name AS region_name, published.description as published_name');
        $query->joinWith(['region']);
        $query->joinWith(['published0']);

      

Now I have added sorting for 2 additional columns

$dataProvider->setSort([
            'attributes' => [
                'name',
                'description',
                'survey_type',
                'published_name' => [
                    'asc' => ['published.description' => SORT_ASC],
                    'desc' => ['published.description' => SORT_DESC],
                    'label' => 'Published',
                    'default' => SORT_ASC
                ],
                'region_name' => [
                    'asc' => ['regions.name' => SORT_ASC],
                    'desc' => ['regions.name' => SORT_DESC],
                    'label' => 'Region'
                ]
            ]
        ]);

      

I am not sure how to do the filtering for these two columns. next thing i

 $query->andFilterWhere([
            'survey_id' => $this->survey_id,
            'published' => $this->published,
            'date_added' => $this->date_added,
            'total_length' => $this->total_length,
            'region_id' => $this->region_id,
            'shrink' => $this->shrink,
            'country_id' => $this->country_id,
            'region_name' => $this->region_name,
            'published_name' => $this->published_name
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'description', $this->description])
            ->andFilterWhere(['like', 'guid', $this->guid])
            ->andFilterWhere(['like', 'year_acquired', $this->year_acquired])
            ->andFilterWhere(['like', 'year_processed', $this->year_processed])
            ->andFilterWhere(['like', 'survey_type', $this->survey_type])
            ->andFilterWhere(['like', 'processing_type', $this->processing_type])
            ->andFilterWhere(['like', 'center_point', $this->center_point])
            ->andFilterWhere(['like', 'regions.name', $this->region_name])
            ->andFilterWhere(['like', 'published.description', $this->published_name]);

      

I have also included public variables in the model Surveys

 public $region_name;
     public $published_name;

      

There gridview

are 2 columns appearing in mine and I can sort them, however there is no input field to filter. how can i filter this?

+3


source to share


1 answer


Variables must be filtered by at least one rule.

Try adding this to your search model:

rules()
{
    return [
        ...
        [['region_name', 'published_name'], 'string', 'max' => 255]
    ];
}

      



From the docs :

When this property is set, the grid will allow column-based filtering. Each data column displays a text box at the top by default that users can fill in to filter data.

Note that in order to display the input field for filtering, the column must have its own attribute yii \ grid \ DataColumn :: $ or set yii \ grid \ DataColumn :: $ filter as the HTML for the input field.

If this property is not set (null), the filtering feature is disabled.

+1


source







All Articles