4
votes

In Yii2 we have GridView like this:

<?= GridView::widget([
        'dataProvider' => $dataProvider,
      //  'filterModel' => $searchModel,
        'layout' => "{items}\n{summary}\n{pager}",
        'columns' => [
         //   ['class' => 'yii\grid\SerialColumn'],

            'id',                
            'size',               
            'program' => [

                'label' => 'Program',
                'value' => function($data)
                    {
                       return Html::a($data->program, ($data->program), ['target' => '_blank']);
                    },
                'format' => 'raw',

            ],

             'version',
             'platform',                 
             'license',                

            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>

Is it possible to do to hide/show column, if we click, for example on button "Hide platform", then for show "Show platform", or maybe checkbox.

I cannot understand how to do this, help me please

3

3 Answers

6
votes

You can do something like this: - Name the column you want to handle, e.g. an ID

[
    'class'          => 'yii\grid\SerialColumn',
    'options' => [ 'id' => 'serial-column' ],
    'width'          => '1%',
    'vAlign'         => 'middle',
    'hAlign'         => 'right',
]
  • Then you modify css to have that column disappeared at the beginning

    #serial-column {display: none}

  • Then you apply js for a checkbox to make it appear:

    jQuery('#some-chkbox').click(function(){ jQuery('#serial-column').toggle(); })

3
votes

Yes, you can hide and show column conditionally using "Visible" Attribute.

[
  'attribute' => 'email',
  'label' => 'Email',
  'visible' => ($_GET['type']) == 'b') ? true : false,
 ],
0
votes

I believe this is what you are looking for.

In short - you can add custom links and script to toggle columns of the gridview table.