关于yii2中的GridView过滤器

时间:2018-01-25 11:42:29

标签: php yii2 yii2-advanced-app yii2-basic-app

我有一个这样的表,有5列。

TableName
-Column1
-Column2
-Column3
-Column4
-Column5

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

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

如果有人可以帮助它会很棒, 感谢。

更新:

$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是文本。

修改:

$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: 这是查询在合并列之前的样子。

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

1 个答案:

答案 0 :(得分:0)

我将添加一个使用countries表来显示GridView数据的示例。

为了达到您的需要,您必须采取以下步骤。

  1. SearchModel
  2. 中创建自定义属性/字段
  3. 将字段添加到gridview列。
    • 在规则中将字段定义为safe
  4. 更新search()内的SearchModel功能,根据新的自定义字段进行搜索和比较。
  5. 假设我有一个包含2个模型的Countries

    1. Countries
    2. CountriesSearch
    3. countries表格包含namecode字段,我想在单个列中显示国家/地区namecode,如下所示。

      enter image description here

      我希望gridview上面的过滤字段能够搜索我是否输入namecode任何一个。

      $combo

      中添加字段名称CountrieSearch
      public $combo
      

      将该字段添加为规则为安全

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

      将新字段添加到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'],
      ],
      ]); ?>
      

      然后更新搜索功能,并在$dataProvider方法中返回search()之前添加以下行

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

      希望这可以帮助你

相关问题