关于 yii2 中的 GridView 过滤器 [英] Regarding GridView Filter in yii2

查看:59
本文介绍了关于 yii2 中的 GridView 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样有 5 列的表格.

I have a table like this with 5 columns.

TableName
-Column1
-Column2
-Column3
-Column4
-Column5

我已合并它们以在网格视图中将它们显示为单列.

I had merged them to display them in grid view as single column.

问题如何根据用户输入进行过滤条件查询以搜索它们.?例如,用户输入一些东西作为输入,它从所有 5 列中搜索并根据搜索输入返回结果.(排序工作正常,请帮我过滤)

Question How can i make filter condition query to search them based on user input.? Eg.User types something as input, it have search from all the 5 columns and return the result based on the search input.(Sorting works fine, please help me with filtering)

如果有人能帮我就好了,谢谢.

If someone could helpme it would be great, Thanks.

更新:

$query->andFilterWhere(['ilike', '"x"."y"', $this->variantName])     ->andFilterWhere(['"a"."b"' => $this->ClassId])
->andFilterWhere(['"c"."d"' => $this->FamilyId]) 
->andFilterWhere(['"e"."f"' => $this->PlatformId]) 
->andFilterWhere(['ilike', '"g"."h"', $this->subFamilyName])

这就是我的旧模型的样子,其中 familyId、classId、PlatformId 的字段是整数,而 subfamilyname、variantname 是文本.

This is how my old model looks like the fields with familyId,classId,PlatformId are integer and subfamilyname,variantname are text.

修改:

$query->andFilterWhere(['or',
                ['ilike', '"x"."y"', $this->Combo],
                ['"a"."b"' => $this->Combo],
                ['"c"."d"' => $this->Combo],
                ['"e"."f"' => $this->Combo],
                ['ilike', '"g"."h"', $this->Combo],
                ])

更新2:这是合并列之前查询的外观.

UPDATE 2: This is how the query looked before merging columns.

->andFilterWhere(['ilike', '"storeNames"."variantName"', $this->variantName])
              ->andFilterWhere(['"storeNames"."classId"' => $this->malwareClassId])
              ->andFilterWhere(['"storeNames"."familyId"' => $this->malwareFamilyId])
              ->andFilterWhere(['"storeNames"."platformId"' => $this->malwarePlatformId])
             ->andFilterWhere(['ilike', '"storeNames"."subFamilyName"', $this->subFamilyName]);

推荐答案

我将添加一个示例,该示例使用国家/地区表通过 GridView 显示数据.

I will add an example that uses a countries table to display data with GridView.

为了完成您需要的操作,您必须执行以下步骤.

In order to accomplish what you need you have to take the following steps.

  1. 在您的 SearchModel 中创建自定义属性/字段.
  2. 将该字段添加到 gridview 列.
    • 在规则中将字段定义为 safe.
  1. Create a custom attribute/field inside your SearchModel.
  2. Add the field to gridview column.
    • Define the field as safe inside the rules.

假设我有一个带有 2 个模型的 Countries

Let's say I have a Countries table with 2 models

  1. 国家
  2. CountriesSearch

countries 表有 namecode 字段,我想显示国家 name代码在一个列中,如下所示.

The countries table has name and code field and i want to show country name and code inside a single column like below.

并且我希望 gridview 上面的过滤器字段应该能够搜索我是否输入 namecode 中的任何一个.

and i want that the filter field above on the gridview should be able to search if i type name or code any one of them.

CountrieSearch

public $combo

将字段添加到规则中是安全的

add the field to the rules as safe

 public function rules() {
        return [
            [ [ 'id' ] , 'integer' ] ,
            [ [ 'name' , 'code' , 'combo' ] , 'safe' ] ,
        ];
    }

将新字段添加到 gridview

Add the new field to the gridview

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
    ['class' => 'yii\grid\SerialColumn'],

    [
        'attribute'=>'combo',
        'label'=>'Name/Code',
        'value'=>function($model){
            return '<span class="label label-success">'.$model->name.'</span><span class="label label-info">('.$model->code.')</span>';
        },
        'format'=>'raw',
    ],

    ['class' => 'yii\grid\ActionColumn'],
],
]); ?>

然后更新搜索函数并在您返回 search() 方法中的 $dataProvider 之前添加以下几行

Then update the search function and add the following lines just before you are returning the $dataProvider inside the search() method

 $query->andFilterWhere ( [ 'OR' ,
            [ 'like' , 'name' , $this->combo ],
            [ 'like' , 'code' , $this->combo ],
        ] );

希望对你有帮助

这篇关于关于 yii2 中的 GridView 过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆