OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

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

  • Thread starter Thread starter copinette
  • Start date Start date
C

copinette

Guest
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:

Code:
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:

Code:
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:

Code:
table = define_table(roles)(queryset)

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

Code:
{"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.
<p>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 <a href="https://stackoverflow.com/questions...on-class-i-e-django-tables2/14134574#14134574">here</a> and it works but with a little problem. Here's the class with a table I'm extending dynamically:</p>

<pre><code>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
</code></pre>

<p>And here is the method that extends the class:</p>

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

<p>When I'm creating a table in views.py like this:</p>

<pre><code>table = define_table(roles)(queryset)
</code></pre>

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

<pre><code>{"class": "paleblue", "orderable":"True", "width":"100%"}
</code></pre>

<p>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.</p>
 
Top