title: Yii2: How to use conditions inside GridView Widget
body: <p>I have a Gridview Widget in my project with <code>'filterModel' =&gt; $searchModel,</code> that shows the user a filter fields and the action column for loading action buttons. My problem is, I want to reuse this Gridview with two separate actions of the controller. One action i.e. the <code>index</code> action loads the data for the user and allows the user to filter using the filter fields and do various actions in the action column. The other <code>controller action</code> is for reports. This produces a pdf report and I don't want the filter fields and the action column to be there in the reports. 
  Is there a way of passing a condition to the Gridview without having to rewrite the whole code using the <code>if else</code> because I find this ineffective in terms of amount of code used. Here is the code I have written so far but need a shorter form with minimal code.</p>
  
  <p>index.php</p>
  
  <pre><code>&lt;?php 
  
  $action_id = Yii::$app-&gt;controller-&gt;action-&gt;id;
  
  if ($action_id == 'index') {
      \yiister\adminlte\widgets\grid\GridView::widget([
          'dataProvider' =&gt; $dataProvider,
          'filterModel' =&gt; $searchModel,
          "condensed" =&gt; false,
          "hover" =&gt; true,
          'columns' =&gt; [
              ['class' =&gt; 'yii\grid\SerialColumn'],
              'name',
              'mobile_number',
              'sex',
              [ 
                  // the attribute 
                  'attribute' =&gt; 'date', 
                  // format the value 
                  'value' =&gt; function ($model) { 
                      if (extension_loaded('intl')) { 
                          return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model-&gt;date]); 
                      } else { 
                          return date($model-&gt;date); 
                      } 
                  }, 
                  // some styling? 
                  'headerOptions' =&gt; [ 
                      'class' =&gt; 'col-md-2' 
                  ], 
                  // here we render the widget 
                  'filter' =&gt; DateRangePicker::widget([ 
                      'model' =&gt; $searchModel, 
                      'attribute' =&gt; 'date_range',
                      'pluginOptions' =&gt; [ 
                          'format' =&gt; 'd-m-Y', 
                          'autoUpdateInput' =&gt; false 
                      ] 
                  ]) 
              ],
              [
              'attribute' =&gt; 'officer',
              'value' =&gt; 'officer.first_name'
              ],
              ['class' =&gt; 'yii\grid\ActionColumn',
               'template' =&gt; '{view}&amp;nbsp{update}',   //{view}&amp;nbsp;
               'buttons' =&gt; [
                   'view' =&gt; function($url, $model)   {
                          return Html::a('&lt;button class="btn btn-success"&gt;&lt;i class="glyphicon glyphicon-eye-open"&gt;&lt;/i&gt;&lt;/button&gt;',$url, [
                              'class' =&gt; 'showModalButton',
                              'id' =&gt; 'lead-view',
                              'title' =&gt; 'View Customer Details',
                              'value' =&gt; $url,
                              'data-toggle' =&gt; 'modal',
                              'data-target' =&gt; 'modal',
                            ]);
                      },
                   'update' =&gt; function($url, $model) {
                          return Html::a('&lt;button class="btn btn-primary"&gt;&lt;i class="glyphicon glyphicon-pencil"&gt;&lt;/i&gt;&lt;/button&gt;',$url, [
                                  'title' =&gt; 'Edit Customer Details',
                              ]);
                      },
                  'urlCreator' =&gt; 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' =&gt; $dataProvider,
          "condensed" =&gt; false,
          "hover" =&gt; true,
          'columns' =&gt; [
              ['class' =&gt; 'yii\grid\SerialColumn'],
              'name',
              'mobile_number',
              'sex',
              [ 
                  // the attribute 
                  'attribute' =&gt; 'date', 
                  // format the value 
                  'value' =&gt; function ($model) { 
                      if (extension_loaded('intl')) { 
                          return Yii::t('app', '{0, date, MMMM dd, YYYY HH:mm}', [$model-&gt;date]); 
                      } else { 
                          return date($model-&gt;date); 
                      } 
                  }, 
                  // some styling? 
                  'headerOptions' =&gt; [ 
                      'class' =&gt; 'col-md-2' 
                  ], 
                  // here we render the widget 
                  'filter' =&gt; DateRangePicker::widget([ 
                      'model' =&gt; $searchModel, 
                      'attribute' =&gt; 'date_range',
                      'pluginOptions' =&gt; [ 
                          'format' =&gt; 'd-m-Y', 
                          'autoUpdateInput' =&gt; false 
                      ] 
                  ]) 
              ],
              [
              'attribute' =&gt; 'officer',
              'value' =&gt; 'officer.first_name'
              ],
          ],
      ]);
  }
  </code></pre>