Django-tables2 - dynamically adding columns to table - not adding attrs to table tag in html
Asked Answered
A

2

9

In my Django project I need to have tables which columns are dynamic and depend on what is in the database. So I found a solution in here and it works but with a little problem. Here's the class with a table I'm extending dynamically:

class ClientsTable(tables.Table):
    class Meta:
        model = Client
        attrs = {"class": "paleblue", "orderable":"True", "width":"100%"}
        fields = ('name',)

    def __init__(self, *args, **kwargs):
        super(ClientsTable, self).__init__(*args, **kwargs)
        self.counter = itertools.count()

    def render_row_number(self):
        return '%d' % next(self.counter)

    def render_id(self, value):
        return '%s' % value

And here is the method that extends the class:

def define_table(roles):
    attrs = dict((r.name, tables.Column() for r in roles)
    klass = type('DynamicTable', (ClientsTable,), attrs)
    return klass

When I'm creating a table in views.py like this:

table = define_table(roles)(queryset)

The table shows columns like I wanted, but in the html code I see that it ignored the attrs:

{"class": "paleblue", "orderable":"True", "width":"100%"}

So there is no css style for paleblue, which is important to me. I feel that it might be something with Meta class but fields and model are working, so I have no idea why attrs are not.

Almaraz answered 22/5, 2013 at 15:40 Comment(0)
M
8

First of all, meta options are not inherited in django-tables2. So you may check the workarounds discussed in the issue to see if something fits or if you can manually add a Meta class to your dynamic table. To do that, you can write your define_table method like this:

def define_table(roles):
    attrs = dict((r.name, tables.Column() for r in roles)
    attrs['Meta'] = type('Meta', (), dict(attrs={"class":"paleblue", "orderable":"True", "width":"100%"}) )
    klass = type('DynamicTable', (ClientsTable,), attrs)
    return klass
Measureless answered 24/5, 2013 at 18:44 Comment(2)
Nice stuff! Not exactly addressing my issue, but I could modify your snippet to solve my problem and learned a lot about Python type and meta class action. Thx a bunch!Fawn
Glad I helped! I've also written various posts doing stuff with type at my blog: spapas.github.io/category/django.htmlMeasureless
A
11

For anyone looking for this now, from django-tables2 1.10 you add columns dynamically to a table by passing extra_columns to the Table constructor.

extra_columns should be a list of tuples, defining a column name and a Column object, eg.

class MyTable(Table):
    static_column = Column()

mytable = MyTable(extra_columns=[('dynamic_column', Column())]

See the API documentation at: http://django-tables2.readthedocs.io/en/latest/pages/api-reference.html#django_tables2.tables.Table

Annuity answered 8/8, 2017 at 14:34 Comment(0)
M
8

First of all, meta options are not inherited in django-tables2. So you may check the workarounds discussed in the issue to see if something fits or if you can manually add a Meta class to your dynamic table. To do that, you can write your define_table method like this:

def define_table(roles):
    attrs = dict((r.name, tables.Column() for r in roles)
    attrs['Meta'] = type('Meta', (), dict(attrs={"class":"paleblue", "orderable":"True", "width":"100%"}) )
    klass = type('DynamicTable', (ClientsTable,), attrs)
    return klass
Measureless answered 24/5, 2013 at 18:44 Comment(2)
Nice stuff! Not exactly addressing my issue, but I could modify your snippet to solve my problem and learned a lot about Python type and meta class action. Thx a bunch!Fawn
Glad I helped! I've also written various posts doing stuff with type at my blog: spapas.github.io/category/django.htmlMeasureless

© 2022 - 2024 — McMap. All rights reserved.