dongyue1988 2018-05-20 16:34
浏览 92
已采纳

Yii2中的GridView过滤器模型

I have a gridview with a dataprovider which has joined two tables as query:

 $applicationDataProvider = new ActiveDataProvider([
                'query' => Application::find()->with('applicantInfo')
                    ->where(['job_posting_id' => $jobPosting->id,
                        'deleted_flag' => 0])->orderBy('created_at desc'),
                'pagination' => [
                    'pageSize' => 5
                ]
            ]);

and here is my GridView:

<?= GridView::widget([
                'dataProvider' => $applicationDataProvider,
                'filterModel'=>$searchModel,
                'columns' => [
                    'id',
                    [
                        'class' => yii\grid\DataColumn::className(),
                        'headerOptions' => ['style' => 'color:#337ab7'],
                        'header' => 'სახელი',
                        'label'=>'name',
                        'value' => function ($data) {
                            return $data->applicantInfo->first_name . ' ' . $data->applicantInfo->last_name;
                        }
                    ],


                    'applicantInfo.email',
                    'applicantInfo.phone',
                    'created_at:date',
                    [
                        'class' => 'yii\grid\ActionColumn',
                        'headerOptions' => ['style' => 'color:#337ab7'],
                        'template' => '{view}',
                        'buttons' => [
                            'view' => function ($url, $model) {
                                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, [
                                    'title' => Yii::t('app', 'გახსნა'),
                                ]);
                            }


                        ],
                        'urlCreator' => function ($action, $model, $key, $index) {
                            if ($action === 'view') {
                                $url = '/app-info/?id=' . $model->id;
                                return $url;
                            }

                        }

 ...

in a search model I have email field only because I want to gridview has just only email search field.

class ApplicationSearch extends Application
{


    public function rules()
    {
        return [

            [['email'], 'safe'],
            ];
    }

but here is not drawn search field for email, how can I fix it?

  • 写回答

1条回答 默认 最新

  • doucu7330 2018-05-20 19:02
    关注

    You need to add email attribute to ApplicationSearch to store value from filter:

    class ApplicationSearch extends Application {
    
        public $email;
    
        public function rules() {
            return [
                [['email'], 'safe'],
            ];
        }
    }
    

    Use this attribute for filtering:

    $applicationDataProvider = new ActiveDataProvider([
        'query' => Application::find()->with('applicantInfo')
            ->where(['job_posting_id' => $jobPosting->id, 'deleted_flag' => 0])
            ->andFilterWhere(['like', 'applicantInfo.email', $this->email])
            ->orderBy('created_at desc'),
        'pagination' => [
            'pageSize' => 5
        ]
    ]);
    

    And use value from relation in grid:

    // ...
    [
        'attribute' => 'email',
        'value' => function ($data) {
            return $data->applicantInfo->email;
        },
    ],
    'applicantInfo.phone',
    // ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥15 有一台三相异步电动机 M1控制一个小车,有四个控制按钮,一个复位、一个启动、一个停止,还有一个急停,两个行程开关分别为上限位和下限位。 上限位。 下限位。
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?