0
我的項目中有一個Gridview窗口小部件,其中'filterModel' => $searchModel,
向用戶顯示了一個過濾器字段和用於加載動作按鈕的動作列。我的問題是,我想通過控制器的兩個獨立的操作來重用此Gridview。一個動作,即index
操作會加載用戶的數據,並允許用戶使用過濾器字段進行過濾,並在動作列中執行各種操作。其他controller action
用於報告。這會生成一個pdf報告,我不希望過濾器字段和操作列出現在報告中。 有沒有辦法將條件傳遞給Gridview,而無需使用if else
重寫整個代碼,因爲我發現這在使用的代碼量方面是無效的。這是我迄今爲止編寫的代碼,但需要使用最少代碼的簡短格式。Yii2:如何使用GridView中的條件窗口小部件
的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'
],
],
]);
}
謝謝尤皮克。你剛剛救了我很多我的朋友。這對我來說很好。 – japheth