相关列

时间:2015-04-22 12:07:28

标签: gridview yii2

我已经使用

在我的模型搜索中加入了两个表格
$query = Surveys::find()->select('surveys.*,regions.name AS region_name, published.description as published_name');
        $query->joinWith(['region']);
        $query->joinWith(['published0']);

我现在已经添加了2个额外列的排序

$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'
                ]
            ]
        ]);

我不确定如何对这两列进行过滤。以下就是我的

 $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]);

我还在Surveys模型

中包含了公共变量
 public $region_name;
     public $published_name;

在我的gridview上出现2列,我可以对它们进行排序,但是没有用于过滤的输入框。我怎么过滤这个?

1 个答案:

答案 0 :(得分:1)

必须通过至少一个规则过滤变量。

尝试将此添加到您的搜索模型

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

来自docs

  

设置此属性后,网格视图将启用基于列的过滤。默认情况下,每个数据列都会在顶部显示一个文本字段,用户可以填写该字段以过滤数据。

     

请注意,为了显示用于过滤的输入字段,列必须设置其yii \ grid \ DataColumn :: $属性属性,或者将yii \ grid \ DataColumn :: $ filter设置为输入的HTML代码字段。

     

如果未设置此属性(null),则禁用过滤功能。