Yii2:如何在 GridView Widget 中使用条件
Yii2: How to use conditions inside GridView Widget
我的项目中有一个带有 'filterModel' => $searchModel,
的 Gridview 小部件,它向用户显示筛选字段和用于加载操作按钮的操作列。我的问题是,我想通过控制器的两个独立操作重用这个 Gridview。一个动作,即 index
动作为用户加载数据,并允许用户使用过滤器字段进行过滤,并在操作列中执行各种操作。另一个 controller action
用于报告。这会生成一个 pdf 报告,我不希望过滤器字段和操作列出现在报告中。
有没有一种方法可以将条件传递给 Gridview 而不必使用 if else
重写整个代码,因为我发现这在使用的代码量方面是无效的。这是我到目前为止编写的代码,但需要代码最少的更短形式。
index.php
<?php
$action_id = Yii::$app->controller->action->id;
if ($action_id == 'index') {
\yiister\adminlte\widgets\grid\GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
"condensed" => false,
"hover" => true,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'mobile_number',
'sex',
[
// the attribute
'attribute' => 'date',
// format the value
'value' => function ($model) {
if (extension_loaded('intl')) {
return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]);
} else {
return date($model->date);
}
},
// some styling?
'headerOptions' => [
'class' => 'col-md-2'
],
// here we render the widget
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'attribute' => 'date_range',
'pluginOptions' => [
'format' => 'd-m-Y',
'autoUpdateInput' => false
]
])
],
[
'attribute' => 'officer',
'value' => 'officer.first_name'
],
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update}', //{view}
'buttons' => [
'view' => function($url, $model) {
return Html::a('<button class="btn btn-success"><i class="glyphicon glyphicon-eye-open"></i></button>',$url, [
'class' => 'showModalButton',
'id' => 'lead-view',
'title' => 'View Customer Details',
'value' => $url,
'data-toggle' => 'modal',
'data-target' => 'modal',
]);
},
'update' => function($url, $model) {
return Html::a('<button class="btn btn-primary"><i class="glyphicon glyphicon-pencil"></i></button>',$url, [
'title' => 'Edit Customer Details',
]);
},
'urlCreator' => function($action, $model, $key, $index) {
if ($action == 'view') {
return Html::a('Action', $url);
}
if ($action == 'update') {
return Html::a('Action', $url);
}
}
],
], // fin ActionColumn
],
]);
} else {
\yiister\adminlte\widgets\grid\GridView::widget([
'dataProvider' => $dataProvider,
"condensed" => false,
"hover" => true,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'mobile_number',
'sex',
[
// the attribute
'attribute' => 'date',
// format the value
'value' => function ($model) {
if (extension_loaded('intl')) {
return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]);
} else {
return date($model->date);
}
},
// some styling?
'headerOptions' => [
'class' => 'col-md-2'
],
// here we render the widget
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'attribute' => 'date_range',
'pluginOptions' => [
'format' => 'd-m-Y',
'autoUpdateInput' => false
]
])
],
[
'attribute' => 'officer',
'value' => 'officer.first_name'
],
],
]);
}
如果条件为:
,则使用简短语法隐藏过滤器
'filterModel' => $myCondition ? $searchModel : null,
要隐藏 ActionColumn
使用 visible
属性:
[
'class' => 'yii\grid\ActionColumn',
'visible' => $myCondition ? true : false,
// here rest of your code
],
我的项目中有一个带有 'filterModel' => $searchModel,
的 Gridview 小部件,它向用户显示筛选字段和用于加载操作按钮的操作列。我的问题是,我想通过控制器的两个独立操作重用这个 Gridview。一个动作,即 index
动作为用户加载数据,并允许用户使用过滤器字段进行过滤,并在操作列中执行各种操作。另一个 controller action
用于报告。这会生成一个 pdf 报告,我不希望过滤器字段和操作列出现在报告中。
有没有一种方法可以将条件传递给 Gridview 而不必使用 if else
重写整个代码,因为我发现这在使用的代码量方面是无效的。这是我到目前为止编写的代码,但需要代码最少的更短形式。
index.php
<?php
$action_id = Yii::$app->controller->action->id;
if ($action_id == 'index') {
\yiister\adminlte\widgets\grid\GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
"condensed" => false,
"hover" => true,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'mobile_number',
'sex',
[
// the attribute
'attribute' => 'date',
// format the value
'value' => function ($model) {
if (extension_loaded('intl')) {
return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]);
} else {
return date($model->date);
}
},
// some styling?
'headerOptions' => [
'class' => 'col-md-2'
],
// here we render the widget
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'attribute' => 'date_range',
'pluginOptions' => [
'format' => 'd-m-Y',
'autoUpdateInput' => false
]
])
],
[
'attribute' => 'officer',
'value' => 'officer.first_name'
],
['class' => 'yii\grid\ActionColumn',
'template' => '{view} {update}', //{view}
'buttons' => [
'view' => function($url, $model) {
return Html::a('<button class="btn btn-success"><i class="glyphicon glyphicon-eye-open"></i></button>',$url, [
'class' => 'showModalButton',
'id' => 'lead-view',
'title' => 'View Customer Details',
'value' => $url,
'data-toggle' => 'modal',
'data-target' => 'modal',
]);
},
'update' => function($url, $model) {
return Html::a('<button class="btn btn-primary"><i class="glyphicon glyphicon-pencil"></i></button>',$url, [
'title' => 'Edit Customer Details',
]);
},
'urlCreator' => function($action, $model, $key, $index) {
if ($action == 'view') {
return Html::a('Action', $url);
}
if ($action == 'update') {
return Html::a('Action', $url);
}
}
],
], // fin ActionColumn
],
]);
} else {
\yiister\adminlte\widgets\grid\GridView::widget([
'dataProvider' => $dataProvider,
"condensed" => false,
"hover" => true,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'name',
'mobile_number',
'sex',
[
// the attribute
'attribute' => 'date',
// format the value
'value' => function ($model) {
if (extension_loaded('intl')) {
return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model->date]);
} else {
return date($model->date);
}
},
// some styling?
'headerOptions' => [
'class' => 'col-md-2'
],
// here we render the widget
'filter' => DateRangePicker::widget([
'model' => $searchModel,
'attribute' => 'date_range',
'pluginOptions' => [
'format' => 'd-m-Y',
'autoUpdateInput' => false
]
])
],
[
'attribute' => 'officer',
'value' => 'officer.first_name'
],
],
]);
}
如果条件为:
,则使用简短语法隐藏过滤器'filterModel' => $myCondition ? $searchModel : null,
要隐藏 ActionColumn
使用 visible
属性:
[
'class' => 'yii\grid\ActionColumn',
'visible' => $myCondition ? true : false,
// here rest of your code
],