django-tables2 linkcolumn多个项目在同一个单元格中 [英] django-tables2 linkcolumn multiple items in the same cell

查看:163
本文介绍了django-tables2 linkcolumn多个项目在同一个单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 tables.LinkColumn 添加多个项目到同一个单元格。

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

这样的东西:

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 += ...

这是否可以?或者我应该创建自己的表视图,没有 django-tables

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

谢谢!

推荐答案

您有两个选项,请使用 TemplateColumn ,或者写一个 render_FOO 方法。

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

以下是使用 TemplateColumn 的示例(可以看到将该记录添加到用于呈现模板的上下文中,从而允许您通过 record.pk 访问 pk

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)

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))

正如你可以看到的,code> TemplateColumn 方法可能在
中有点清洁案例。

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

这篇关于django-tables2 linkcolumn多个项目在同一个单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆