django-tables2 icon indicating copy to clipboard operation
django-tables2 copied to clipboard

column.attrs and render behaviour question regarding table headers

Open xtlc opened this issue 4 years ago • 0 comments

Following an example in my code where I tried to optimize in terms of DRY:

class CustomColumn(django_tables2.Column):
    def render(self, value, column):
        column.attrs = {"td": {"align": "right"},}              ### <-- visible in the page source later on
                           #  "th": {"style": "text-align:right"}} ### <-- not visible in the page source later on
        if value <= 0:    
            column.attrs.update({"td": {"align": "right", "style": "color: red"}})   ### <-- visible in the page source later on
        return "{:0.2f}".format(value)


class SomeTable(django_tables2.Table):
    number = CustomColumn(accessor="get_number", 
                                    verbose_name="number",
                                    orderable=False,
                                    attrs = {"th": {"style": "text-align:right"}},)   ### <-- visible in the page source later on

    name = django_tables2.LinkColumn("name", args=[django_tables2.A("pk")])
    
    class Meta:
        model = SomeModel
        sequence = ("name", "number",)
        exclude = ("id",)
        attrs = {"class": "table table-hover table-striped table-borderless",}

Is this the expected behaviour? Why can't I set the

options in the render method?

xtlc avatar Jul 23 '20 14:07 xtlc