【问题标题】:GridView filter model in Yii2Yii2中的GridView过滤器模型
【发布时间】:2018-05-20 16:34:04
【问题描述】:

我有一个带有 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
                ]
            ]);

这是我的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;
                            }

                        }

 ...

搜索模型中我只有 email 字段,因为我想 gridview 只有电子邮件搜索字段。

class ApplicationSearch extends Application
{


    public function rules()
    {
        return [

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

但是这里没有为电子邮件绘制搜索字段,我该如何解决?

【问题讨论】:

  • is email 是您的表架构的一部分,这意味着它是一个实际的字段吗?还是仅适用于模型的公共属性?
  • 是申请者信息表的列,不是申请表的列
  • 在这种情况下,下面给出的答案应该对你有用吗?
  • 不,它没有用。 . .
  • 看,我更新了我的问题并在我的sn-p代码中添加了列,也许它可以帮助你回答

标签: php mysql gridview yii2


【解决方案1】:

您需要将email 属性添加到ApplicationSearch 以存储来自过滤器的值:

class ApplicationSearch extends Application {

    public $email;

    public function rules() {
        return [
            [['email'], 'safe'],
        ];
    }
}

使用此属性进行过滤:

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

并使用网格中关系的值:

// ...
[
    'attribute' => 'email',
    'value' => function ($data) {
        return $data->applicantInfo->email;
    },
],
'applicantInfo.phone',
// ...

【讨论】:

  • 出现错误:“设置未知属性:yii\grid\DataColumn::name”
  • 应该是attriibute
  • @MuhammadOmerAslam 是的,刚刚修好了。 @godot 尝试更新版本。
  • 这个答案应该可以工作,因为它也包含搜索的相关记录过滤器。
猜你喜欢
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 2020-05-23
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
相关资源
最近更新 更多