简体   繁体   English

Yii2中的GridView过滤器模型

[英]GridView filter model in Yii2

I have a gridview with a dataprovider which has joined two tables as query: 我有一个带有dataprovider的gridview,它已将两个表作为查询联接:

 $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

<?= 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. 搜索模型中,我仅具有电子邮件字段,因为我想要gridview仅具有电子邮件搜索字段。

class ApplicationSearch extends Application
{


    public function rules()
    {
        return [

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

but here is not drawn search field for email, how can I fix it? 但是这里没有为电子邮件绘制搜索字段,我该如何解决?

You need to add email attribute to ApplicationSearch to store value from filter: 您需要向ApplicationSearch添加email属性以存储来自过滤器的值:

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',
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM