gpt4 book ai didi

php - Yii2 中的 GridView 过滤器模型

转载 作者:行者123 更新时间:2023-11-29 04:33:50 27 4
gpt4 key购买 nike

我有一个带有数据提供者的 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;
}

}

...

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

class ApplicationSearch extends Application
{


public function rules()
{
return [

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

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

最佳答案

您需要将 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',
// ...

关于php - Yii2 中的 GridView 过滤器模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50437058/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com