This snippet provides the ability to not only add custom conditions to which grid field columns are visible, but change their order on the fly.
According to the statamic documentation
Important: Conditionals work on top-level fields only, not meta fields (fields that include other fields) like Replicator, Bard, and Grid.
They make it sound like it's just not possible with these field types. Which is mostly true... But with a bit of work you can write your own custom ones by extending Statamic.conditions
in your site/helpers/cp/scripts.js
When extending the Statamic.conditions
you're not given very much information about the condition's environment other than the entire entry fields object. So you have to get tricky by manually querying the dom to get other field values and handling it that way. But when you query the fields, you can gain access to the vue component properties and modify the field settings.
The script provides a gridColumnFilters
condition to be used with show_when
.
Create a grid field and add:
show_when: gridColumnFilters
Create a choice field (suggest, checkboxes... etc, for best results use a suggest) with the name of your grid field + _columns
(mygrid_columns
). Each option should match your grid fields names.
Any grid fields that you want to always be visible add persistent_column: true
to that grid fields settings.
The grid field will only be visible when the _columns
field has value.
The grid fields will also be displayed in the order of the suggest field.
Usage Example: