Django-tables2 - 动态地向表添加列 - 不在html中向表标记添加attrs

时间:2022-02-21 07:24:21

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:

在我的Django项目中,我需要有表,哪些列是动态的,并且取决于数据库中的内容。所以我在这里找到了一个解决方案,它有效,但有一点问题。这是我正在动态扩展的表的类:

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:

当我在views.py中创建一个像这样的表时:

table = define_table(roles)(queryset)

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

该表显示了我想要的列,但在html代码中我看到它忽略了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.

因此paleblue没有css风格,这对我很重要。我觉得它可能是Meta类的东西,但字段和模型正在工作,所以我不知道为什么没有attrs。

2 个解决方案

#1


6  

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 you can manuall add a Meta class to your dynamic table. To do that, you can your define_table method like this:

首先,在django-tables2中不继承元选项。因此,您可以检查问题中讨论的变通方法,看看是否适合,或者您可以手动将Meta类添加到动态表中。要做到这一点,你可以使用这样的define_table方法:

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%"}) )
  return klass

Oops after more than two years I noticed that there was an error in my code -- I'd forgotten to include the line klass = type('DynamicTable', (ClientsTable,), attrs) before return klass above. I'm, adding it now for completeness.

经过两年多的哎呀,我注意到我的代码中有一个错误 - 我忘记在返回上面的klass之前包含行klass = type('DynamicTable',(ClientsTable),attrs)。我现在加入它是为了完整性。

#2


4  

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.

对于现在正在寻找这个的人来说,从django-tables2 1.10你可以通过将extra_columns传递给Table构造函数来动态地将列添加到表中。

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

extra_columns应该是元组列表,定义列名和Column对象,例如。

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

请参阅API文档:http://django-tables2.readthedocs.io/en/latest/pages/api-reference.html#django_tables2.tables.Table

#1


6  

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 you can manuall add a Meta class to your dynamic table. To do that, you can your define_table method like this:

首先,在django-tables2中不继承元选项。因此,您可以检查问题中讨论的变通方法,看看是否适合,或者您可以手动将Meta类添加到动态表中。要做到这一点,你可以使用这样的define_table方法:

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%"}) )
  return klass

Oops after more than two years I noticed that there was an error in my code -- I'd forgotten to include the line klass = type('DynamicTable', (ClientsTable,), attrs) before return klass above. I'm, adding it now for completeness.

经过两年多的哎呀,我注意到我的代码中有一个错误 - 我忘记在返回上面的klass之前包含行klass = type('DynamicTable',(ClientsTable),attrs)。我现在加入它是为了完整性。

#2


4  

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.

对于现在正在寻找这个的人来说,从django-tables2 1.10你可以通过将extra_columns传递给Table构造函数来动态地将列添加到表中。

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

extra_columns应该是元组列表,定义列名和Column对象,例如。

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

请参阅API文档:http://django-tables2.readthedocs.io/en/latest/pages/api-reference.html#django_tables2.tables.Table