1
0
Fork 0

feat: add auto-link (to "View") behavior for grid columns

This commit is contained in:
Lance Edgar 2024-08-10 16:45:12 -05:00
parent a361f07980
commit e0de4e9a65
3 changed files with 80 additions and 1 deletions

View file

@ -72,6 +72,13 @@ class Grid:
List of :class:`GridAction` instances represenging action links List of :class:`GridAction` instances represenging action links
to be shown for each record in the grid. to be shown for each record in the grid.
.. attribute:: linked_columns
List of column names for which auto-link behavior should be
applied.
See also :meth:`set_link()` and :meth:`is_linked()`.
.. attribute:: vue_tagname .. attribute:: vue_tagname
String name for Vue component tag. By default this is String name for Vue component tag. By default this is
@ -85,12 +92,14 @@ class Grid:
columns=None, columns=None,
data=None, data=None,
actions=[], actions=[],
linked_columns=[],
vue_tagname='wutta-grid', vue_tagname='wutta-grid',
): ):
self.request = request self.request = request
self.key = key self.key = key
self.data = data self.data = data
self.actions = actions or [] self.actions = actions or []
self.linked_columns = linked_columns or []
self.vue_tagname = vue_tagname self.vue_tagname = vue_tagname
self.config = self.request.wutta_config self.config = self.request.wutta_config
@ -122,6 +131,51 @@ class Grid:
""" """
self.columns = FieldList(columns) self.columns = FieldList(columns)
def set_link(self, key, link=True):
"""
Explicitly enable or disable auto-link behavior for a given
column.
If a column has auto-link enabled, then each of its cell
contents will automatically be wrapped with a hyperlink. The
URL for this will be the same as for the "View"
:class:`GridAction`
(aka. :meth:`~wuttaweb.views.master.MasterView.view()`).
Although of course each cell gets a different link depending
on which data record it points to.
It is typical to enable auto-link for fields relating to ID,
description etc. or some may prefer to auto-link all columns.
See also :meth:`is_linked()`; the list is tracked via
:attr:`linked_columns`.
:param key: Column key as string.
:param link: Boolean indicating whether column's cell contents
should be auto-linked.
"""
if link:
if key not in self.linked_columns:
self.linked_columns.append(key)
else: # unlink
if self.linked_columns and key in self.linked_columns:
self.linked_columns.remove(key)
def is_linked(self, key):
"""
Returns boolean indicating if auto-link behavior is enabled
for a given column.
See also :meth:`set_link()` which describes auto-link behavior.
:param key: Column key as string.
"""
if self.linked_columns:
if key in self.linked_columns:
return True
return False
def render_vue_tag(self, **kwargs): def render_vue_tag(self, **kwargs):
""" """
Render the Vue component tag for the grid. Render the Vue component tag for the grid.

View file

@ -10,7 +10,12 @@
label="${column['label']}" label="${column['label']}"
v-slot="props" v-slot="props"
cell-class="c_${column['field']}"> cell-class="c_${column['field']}">
<span v-html="props.row.${column['field']}"></span> % if grid.is_linked(column['field']):
<a :href="props.row._action_url_view"
v-html="props.row.${column['field']}" />
% else:
<span v-html="props.row.${column['field']}"></span>
% endif
</${b}-table-column> </${b}-table-column>
% endfor % endfor

View file

@ -49,6 +49,26 @@ class TestGrid(TestCase):
grid = self.make_grid() grid = self.make_grid()
self.assertEqual(grid.vue_component, 'WuttaGrid') self.assertEqual(grid.vue_component, 'WuttaGrid')
def test_linked_columns(self):
grid = self.make_grid(columns=['foo', 'bar'])
self.assertEqual(grid.linked_columns, [])
self.assertFalse(grid.is_linked('foo'))
grid.set_link('foo')
self.assertEqual(grid.linked_columns, ['foo'])
self.assertTrue(grid.is_linked('foo'))
self.assertFalse(grid.is_linked('bar'))
grid.set_link('bar')
self.assertEqual(grid.linked_columns, ['foo', 'bar'])
self.assertTrue(grid.is_linked('foo'))
self.assertTrue(grid.is_linked('bar'))
grid.set_link('foo', False)
self.assertEqual(grid.linked_columns, ['bar'])
self.assertFalse(grid.is_linked('foo'))
self.assertTrue(grid.is_linked('bar'))
def test_render_vue_tag(self): def test_render_vue_tag(self):
grid = self.make_grid(columns=['foo', 'bar']) grid = self.make_grid(columns=['foo', 'bar'])
html = grid.render_vue_tag() html = grid.render_vue_tag()