Reputation: 275
I have problem here.
I want to add a condition inside the GridView
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'table_column1',
'table_column2',
//CONDITION HERE
if(condition){
//BUTTON HERE
} else {
//TOOLS COLUMN HERE
}
],
]); ?>
How can I do that?
Upvotes: 3
Views: 4751
Reputation: 9
Here is simple if else condition inside gridview
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'event_id',
//'img:ntext',
//'path:ntext',
//'type',
//'description:ntext',
[
'attribute' => 'type',
'value'=> function($data){
if($data->type == 0){
$info = 'Empty';
}
if($data->type == 1){
$info = 'One';
}
if($data->type == 2){
$info = 'Two';
}
return $info;
},
'format'=>'html',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Upvotes: 1
Reputation: 1
[
'class' => 'kartik\grid\EditableColumn',
'attribute' => 'status',
'label'=>'Status',
'filterType' => \kartik\grid\GridView::FILTER_SELECT2,
'filter' => ArrayHelper::map(\app\models\PropertyStatus::find()->all(), 'status', 'status'),
'filterWidgetOptions' => [
'theme' => Select2::THEME_BOOTSTRAP,
'pluginOptions' => [
'allowClear' => true,
],
],
'filterInputOptions' => ['placeholder' => 'All...'],
'format'=>'raw',
//'options' => ['style' => 'width: 8%'],
'editableOptions'=> function ($model, $key, $index, $widget) {
if(Yii::$app->user->can('property/status')){
$appttypes = ArrayHelper::map(\app\models\PropertyStatus::find()->all(), 'status', 'status');
return [
'header' => 'Status',
'attribute' => 'status',
'asPopover' => false,
'inlineSettings' => [
'closeButton' => '<button class="btn btn-sm btn-danger kv-editable-close kv-editable-submit" title="Cancel Edit"><i class="fa fa-times-circle"></i></button>'
],
'type' => 'primary',
//'size'=> 'lg',
'size' => 'md',
'options' => ['class'=>'form-control', 'placeholder'=>'Enter person name...'],
'inputType' => Editable::INPUT_DROPDOWN_LIST,
'displayValueConfig' => $appttypes,
'data' => $appttypes,
'formOptions'=> ['action' => ['/ajax/update-property-status']] // point to new action
];
}
},
],
Upvotes: 0
Reputation: 22174
You can use $visible
property if you want only show/hide some columns based on specified condition:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'table_column1',
'table_column2',
[
'name' => 'some_column1',
'visible' => $condition,
],
[
'name' => 'some_column2',
'visible' => !$condition,
],
],
]); ?>
Based on $condition
, only one of two columns (some_column1
or some_column1
) will be visible.
Upvotes: 7
Reputation: 133400
If you need two gridview one alternative to the other a simple way is based on alternate $column contents
if ( condition) {
$columns = [
['class' => 'yii\grid\SerialColumn'],
'table_column1',
'table_column2',
[
'attribute'=>'your_att',
'header'=>'your_header',
'format'=>'raw',
'value' => function($model, $key, $index)
{
return '<button class="btn green">Y</button>';
},
],
],
} else {
$columns = [
['class' => 'yii\grid\SerialColumn'],
'table_column1',
'table_column2',
['class' => 'yii\grid\ActionColumn',
],
}
}
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $columns
]); ?>
otherwise uisng the value function for a raw column type you could return the code you need for each specific rows ..
Upvotes: 3