From 16131cd2560f57bb8f19022e46abc636e6f4a38c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 14 Mar 2026 16:24:58 -0500 Subject: [PATCH] fix: keep original value along with rendered, in grid vue context this change was made for sake of sorting, when the backend is not responsible for that. in particular datetime values must be "rendered" somehow when passing to frontend, but depending on various factors the rendering may not preserve the "sensible" sort order behavior, e.g. if "weekday name" begins the rendered string. so in all cases now, rendered values will be given a distinct key in the record dict, while the original value stays in its original place. this should let grid sorting work off the original value, and hopefully all is well..fingers crossed --- src/wuttaweb/grids/base.py | 13 +++++-------- src/wuttaweb/templates/grids/vue_template.mako | 4 ++-- tests/grids/test_base.py | 18 ++++++++++++++++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/wuttaweb/grids/base.py b/src/wuttaweb/grids/base.py index 1906997..e2a9f83 100644 --- a/src/wuttaweb/grids/base.py +++ b/src/wuttaweb/grids/base.py @@ -2501,11 +2501,10 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth # loop thru data data = [] row_classes = {} - for i, record in enumerate(original_data, 1): - original_record = record + for i, original_record in enumerate(original_data, 1): # convert record to new dict - record = self.object_to_dict(record) + record = self.object_to_dict(original_record) # discard non-declared fields record = {field: record[field] for field in record if field in self.columns} @@ -2518,19 +2517,17 @@ class Grid: # pylint: disable=too-many-instance-attributes,too-many-public-meth # nb. no need to render if column not included if key in self.columns: value = record.get(key, None) - record[key] = renderer(original_record, key, value) + record[f"_rendered_{key}"] = renderer(original_record, key, value) # add action urls to each record for action in self.actions: key = f"_action_url_{action.key}" if key not in record: - url = action.get_url(original_record, i) - if url: + if url := action.get_url(original_record, i): record[key] = url # set row css class if applicable - css_class = self.get_row_class(original_record, record, i) - if css_class: + if css_class := self.get_row_class(original_record, record, i): # nb. use *string* zero-based index, for js compat row_classes[str(i - 1)] = css_class diff --git a/src/wuttaweb/templates/grids/vue_template.mako b/src/wuttaweb/templates/grids/vue_template.mako index 47720fa..34424e9 100644 --- a/src/wuttaweb/templates/grids/vue_template.mako +++ b/src/wuttaweb/templates/grids/vue_template.mako @@ -177,9 +177,9 @@ cell-class="c_${column['field']}"> % if grid.is_linked(column['field']): + v-html="props.row?._rendered_${column['field']} === undefined ? props.row.${column['field']} : props.row._rendered_${column['field']}" /> % else: - + % endif % endif diff --git a/tests/grids/test_base.py b/tests/grids/test_base.py index 15a7a06..2978364 100644 --- a/tests/grids/test_base.py +++ b/tests/grids/test_base.py @@ -2025,7 +2025,12 @@ class TestGrid(WebTestCase): context, { "data": [ - {"foo": "blah blah", "baz": "zoo", "_action_url_view": "/blarg"} + { + "foo": "bar", + "_rendered_foo": "blah blah", + "baz": "zoo", + "_action_url_view": "/blarg", + } ], "row_classes": {}, }, @@ -2080,7 +2085,16 @@ class TestGrid(WebTestCase): # can override value rendering grid.set_renderer("foo", lambda record, key, value: "blah blah") data = grid.get_vue_data() - self.assertEqual(data, [{"foo": "blah blah", "_action_url_view": "/blarg"}]) + self.assertEqual( + data, + [ + { + "foo": "bar", + "_rendered_foo": "blah blah", + "_action_url_view": "/blarg", + } + ], + ) def test_get_row_class(self): model = self.app.model