Hide GridView Columns

Using either the default GridView or Kartik Gridview, how could I give the user the ability to show/hide the columns to display?

The idea is that the Gridview displays a lot of information and certain users don’t need to see certain column and others do, so I was hoping that there might be a way to select which columns are displayed.

add “visibility” => false inside the column array

How does that enable users to select/deselect columns to display?

You can add if statement and change visibility status false or true according to user’s role or id

I had thought of that, but it will not suffice for this instance. The end-user needs to have the ability to make the selection (and change it if they so choose) in real-time.

@DBCreator you can add some kind of button/filter/link with some flag parameter example the url can be /test?hide_columns=1
And you could simple pass that variable from your action to your view and set it to your gridview, if you don’t want any refresh, you can use pjax, or a simple Ajax request and leload the updated content

1 Like

You can also use checkboxes to select the content the user wants to see

1 Like

Could you elaborate. Are you saying to use javascript/jQuery?

I got it working. jQuery was the ticket to hiding columns, in combination with a multiselect table column list that the user chooses the columns to show/hide. It ended up being quite straightforward at the end of the day. Thank you all for the help.