Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to hide column Yii2?

Tags:

jquery

php

yii2

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

like image 428
Agent Smith Avatar asked Jan 04 '23 06:01

Agent Smith


1 Answers

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(); })

like image 63
Trac Nguyen Avatar answered Jan 12 '23 05:01

Trac Nguyen