DalinDad
DalinDad

Reputation: 103

Change type column in django-tables2

Fill table

mydata.append({
            'bool_access': True,
            'path': path
        })
table = mytable(data=mydata)

----> render table

Table

class mytable(tables.Table):

    path = tables.Column(verbose_name='My path')

    # path = tables.LinkColumn('page_web', args=[A('path')],verbose_name='My path')

    bool_access = ""

    class Meta:
        attrs = {'class': 'table table-bordered table-striped table-condensed'}
        sequence = ('path')

Wanted

I want that if a add a row in my data with bool_access at "True" that the column type for path is tables.LinkColumn and else if bool_access at "False" the column type is tables.Column.

Thanks in advance for any help.

Upvotes: 0

Views: 244

Answers (1)

Jieter
Jieter

Reputation: 4229

There are multiple ways to approach this, but I think the most straightforward one is using a TemplateColumn:

class MyTable(tables.Table):

    path = tables.TemplateColumn(
        verbose_name='My path', 
        template_code="""{% if record.bool_access %}<a href="{% url "page_web" record.path %}">{{ record.path }}</a>{% else %}{{ record.path }}{% endif %}""")

    class Meta:
        attrs = {'class': 'table table-bordered table-striped table-condensed'}
        sequence = ('path')

Upvotes: 1

Related Questions