From 6dc0ff40e728e870fc99a45cfe4713c85a9e7766 Mon Sep 17 00:00:00 2001 From: Christian Alis Date: Sun, 30 Mar 2025 17:31:40 +0800 Subject: [PATCH] Fix issue when different class is defined in attrs for th and td --- .../_partials/bootstrap4_table_block.html | 4 +- .../_partials/bootstrap5_table_block.html | 4 +- .../tests/test_base.py | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap4_table_block.html b/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap4_table_block.html index 2f6e502..75f96a8 100644 --- a/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap4_table_block.html +++ b/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap4_table_block.html @@ -28,7 +28,7 @@ {% if column.attrs.td.class not in table.get_column_excluded %} {% if column.attrs.td.class in table.get_column_default_show %}
  • @@ -48,7 +48,7 @@
  • {% else %}
  • diff --git a/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap5_table_block.html b/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap5_table_block.html index 47a3f7c..76b322a 100644 --- a/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap5_table_block.html +++ b/django_tables2_column_shifter/templates/django_tables2_column_shifter/_partials/bootstrap5_table_block.html @@ -28,7 +28,7 @@ {% if column.attrs.td.class not in table.get_column_excluded %} {% if column.attrs.td.class in table.get_column_default_show %}
  • @@ -48,7 +48,7 @@
  • {% else %}
  • diff --git a/django_tables2_column_shifter/tests/test_base.py b/django_tables2_column_shifter/tests/test_base.py index 19dd937..6cc8148 100644 --- a/django_tables2_column_shifter/tests/test_base.py +++ b/django_tables2_column_shifter/tests/test_base.py @@ -239,3 +239,41 @@ class Meta: self.assertTrue('data-td-class="age"' in render) self.assertFalse('data-td-class="first_name"' in render) self.assertFalse('data-td-class="last_name"' in render) + + def test_class_attrs(self): + """ + Class for targeting should be col name even if class defined in attrs + """ + + for case in self.CASE: + + if ( + case["min_dt_version"] and case["min_dt_version"] > dt_version + ) or ( + case["max_dt_version"] and case["max_dt_version"] < dt_version + ): + continue + + class Tab(case["table_clsss"]): + first_name = tables.Column( + attrs={ + "th": {"class": "text-nowrap"}, + "td": {"class": "text-center"}, + } + ) + + class Meta: + model = Author + + table = Tab(Author.objects.all()) + request = RequestFactory().get("/fake/url") + template = Template( + """ + {% load django_tables2 %} + {% render_table table %} + """ + ) + ctx = Context({"table": table, "request": request}) + render = template.render(ctx) + + self.assertTrue('data-td-class="first_name"' in render)