Allow grid columns to be *invisible* (but still present in grid)

this can be useful when you need contextual data for a given row, for
sake of front-end UI features, but do not want to actually show the
extra data column(s)
This commit is contained in:
Lance Edgar 2021-08-29 10:28:36 -05:00
parent fe584f193f
commit 4d742bacb1
2 changed files with 32 additions and 2 deletions

View file

@ -69,7 +69,7 @@ class Grid(object):
def __init__(self, key, data, columns=None, width='auto', request=None,
model_class=None, model_title=None, model_title_plural=None,
enums={}, labels={}, assume_local_times=False, renderers={},
enums={}, labels={}, assume_local_times=False, renderers={}, invisible=[],
extra_row_class=None, linked_columns=[], url='#',
joiners={}, filterable=False, filters={}, use_byte_string_filters=False,
sortable=False, sorters={}, default_sortkey=None, default_sortdir='asc',
@ -105,6 +105,7 @@ class Grid(object):
self.labels = labels or {}
self.assume_local_times = assume_local_times
self.renderers = self.make_default_renderers(renderers or {})
self.invisible = invisible or []
self.extra_row_class = extra_row_class
self.linked_columns = linked_columns or []
self.url = url
@ -161,13 +162,38 @@ class Grid(object):
return [prop.key for prop in mapper.iterate_properties]
def hide_column(self, key):
"""
This *removes* a column from the grid, altogether.
This method should really be renamed to ``remove_column()``
instead.
"""
if key in self.columns:
self.columns.remove(key)
def hide_columns(self, *keys):
"""
This *removes* columns from the grid, altogether.
This method should really be renamed to ``remove_columns()``
instead.
"""
for key in keys:
self.hide_column(key)
def set_invisible(self, key, invisible=True):
"""
Mark the given column as "invisible" (but do not remove it).
Use :meth:`hide_column()` if you actually want to remove it.
"""
if invisible:
if key not in self.invisible:
self.invisible.append(key)
else:
if key in self.invisible:
self.invisible.remove(key)
def append(self, field):
self.columns.append(field)
@ -1185,6 +1211,7 @@ class Grid(object):
'field': name,
'label': self.get_label(name),
'sortable': self.sortable and name in self.sorters,
'visible': name not in self.invisible,
})
return columns

View file

@ -176,7 +176,10 @@
<template slot-scope="props">
% for column in grid_columns:
<b-table-column field="${column['field']}" label="${column['label']}" ${'sortable' if column['sortable'] else ''}>
<b-table-column field="${column['field']}"
label="${column['label']}"
:sortable="${json.dumps(column['sortable'])}"
:visible="${json.dumps(column['visible'])}">
% if grid.is_linked(column['field']):
<a :href="props.row._action_url_view" v-html="props.row.${column['field']}"></a>
% else: