Is it possible to hide column Yii2?

三世轮回 提交于 2020-02-23 20:18:37

问题


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


回答1:


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




回答2:


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

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



回答3:


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.



来源:https://stackoverflow.com/questions/46971647/is-it-possible-to-hide-column-yii2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!