From a042d511fbd356709401264f4a8081dc48442600 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 21 Aug 2024 15:50:36 -0500 Subject: [PATCH] feat: add "searchable" column support for grids frontend / basic only --- src/wuttaweb/grids/base.py | 38 +++++++++++++++++++ .../templates/grids/vue_template.mako | 1 + src/wuttaweb/views/settings.py | 6 +++ tests/grids/test_base.py | 20 ++++++++++ 4 files changed, 65 insertions(+) diff --git a/src/wuttaweb/grids/base.py b/src/wuttaweb/grids/base.py index 272845e..48117e0 100644 --- a/src/wuttaweb/grids/base.py +++ b/src/wuttaweb/grids/base.py @@ -282,6 +282,12 @@ class Grid: Only relevant if :attr:`paginated` is true. If not specified, constructor will assume ``1`` (first page). + + .. attribute:: searchable_columns + + Set of columns declared as searchable for the Vue component. + + See also :meth:`set_searchable()` and :meth:`is_searchable()`. """ def __init__( @@ -306,6 +312,7 @@ class Grid: pagesize_options=None, pagesize=None, page=1, + searchable_columns=None, ): self.request = request self.vue_tagname = vue_tagname @@ -344,6 +351,9 @@ class Grid: self.pagesize = pagesize or self.get_pagesize() self.page = page + # searching + self.searchable_columns = set(searchable_columns or []) + def get_columns(self): """ Returns the official list of column names for the grid, or @@ -543,6 +553,28 @@ class Grid: return True return False + def set_searchable(self, key, searchable=True): + """ + (Un)set the given column's searchable flag for the Vue + component. + + See also :meth:`is_searchable()`. Flags are tracked via + :attr:`searchable_columns`. + """ + if searchable: + self.searchable_columns.add(key) + elif key in self.searchable_columns: + self.searchable_columns.remove(key) + + def is_searchable(self, key): + """ + Check if the given column is marked as searchable for the Vue + component. + + See also :meth:`set_searchable()`. + """ + return key in self.searchable_columns + def add_action(self, key, **kwargs): """ Convenience to add a new :class:`GridAction` instance to the @@ -1384,8 +1416,13 @@ class Grid: 'field': 'foo', 'label': "Foo", 'sortable': True, + 'searchable': False, } + The full format is determined by Buefy; see the Column section + in its `Table docs + `_. + See also :meth:`get_vue_data()`. """ if not self.columns: @@ -1397,6 +1434,7 @@ class Grid: 'field': name, 'label': self.get_label(name), 'sortable': self.is_sortable(name), + 'searchable': self.is_searchable(name), }) return columns diff --git a/src/wuttaweb/templates/grids/vue_template.mako b/src/wuttaweb/templates/grids/vue_template.mako index cb4f7a8..da87296 100644 --- a/src/wuttaweb/templates/grids/vue_template.mako +++ b/src/wuttaweb/templates/grids/vue_template.mako @@ -53,6 +53,7 @@ label="${column['label']}" v-slot="props" :sortable="${json.dumps(column.get('sortable', False))|n}" + :searchable="${json.dumps(column.get('searchable', False))|n}" cell-class="c_${column['field']}"> % if grid.is_linked(column['field']):