Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

django-tables2 linkcolumn multiple items in the same cell

I'd like to add multiple 'items' to the same cell using tables.LinkColumn.

Something like this:

column_name = tables.LinkColumn('some_url_edit', args=[A('pk')], attrs={'class':'tbl_icon edit'})
column_name += tables.LinkColumn('some_url_del', args=[A('pk')], attrs={'class':'tbl_icon delete'})
column_name += ...

Is this even possible? or should I create my own table view, without django-tables.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have two options here, either use a TemplateColumn, or write a render_FOO method.

Here is an example using the TemplateColumn (as you can see the record is added to the context that is used to render the template, thus allowing you to access the pk via record.pk:

TEMPLATE = '''
   <a href="{% url some_url_edit record.pk %}" class="tbl_icon edit">Edit</a>
   <a href="{% url some_url_del record.pk %}" class="tbl_icon delete">Delete</a>
'''

class MyTable(tables.Table):
    column_name = tables.TemplateColumn(TEMPLATE)

Example using the render_FOO:

from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse

class MyTable(tables.Table):
    column_name = tables.Column()

    def render_column_name(self, record):
        edit_url = reverse("some_url_edit", args=[record.pk])
        del_url = reverse("some_url_del", args=[record.pk])
        return mark_safe('''<a href="%s" class="tbl_icon edit">Edit</a>
                         <a href="%s" class="tbl_icon delete">Delete</a>'''
                         % (edit_url, del_url))

As you can see the TemplateColumn approach is probably a little cleaner in your case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...