Django-tables2呈现合并的表

时间:2013-09-14 17:18:36

标签: python django django-tables2

我使用django-tables2并拥有以下表格,该表格由生成的字典填充:

class SimpleStatTable(tables.Table):
    Metric = tables.Column(accessor='metric')
    Qty = tables.Column(accessor='qty')
    Min = tables.Column(accessor='min')
    Avg = tables.Column(accessor='avg')
    Max = tables.Column(accessor='max')

    def __init__(self, data, label=None, **kwargs):
        self.label = label
        super(SimpleStatTable, self).__init__(data, **kwargs)

    class Meta:
        order_by = ('Metric',)
        empty_text = 'No data presented'

我想渲染一些SimpleStatTable表的合并表。用django-tables2可以吗?我喜欢django-tables2功能,比如排序

我有一小部分example所需的表格。

我想我需要像here一样动态生成合并表的类,但是如何添加额外的合并列呢?

1 个答案:

答案 0 :(得分:0)

我有一个解决方案,但它并不优雅。

我合并了两个字典并为合并字典构建表。之后我只是将合并的列添加到表中。它们与合并字典中的顺序相同。此外,我已更改表格的模板以处理合并列。

以下是代码:

from collections import defaultdict
import django_tables2 as tables

def merge_table_dicts(labels, tables, key_column):
    merged_tables = []
    for table, label in zip(tables, labels):
        new_table = []
        for row in table:
            new_row = {}
            for key, value in row.iteritems():
                if key == key_column:
                    new_row[key] = value
                else:
                    new_row[old_key + '_' + label] = value
            new_table.append(new_row)
        merged_tables.append(new_table)
    d = defaultdict(dict)
    for l in merged_tables:
        for elem in l:
            d[elem[key_column]].update(elem)
    merged_table_dicts = d.values()
    return merged_table_dicts


def get_merged_table(labels, tables_dicts, key_column_name, merged_columns_order):
    attrs = {}
    # General options
    class Meta:
        order_by = (key_column_name,)
        template = '_merged_table.html'
        empty_text = 'No data presented'
    attrs['Meta'] = Meta
    attrs[key_column_name] = tables.Column()
    for column in merged_columns_order:
        for label in labels:
            attrs[get_merged_key(column, label)] = tables.Column(verbose_name=label)
    merged_table = type('MergedTable', (tables.Table,), attrs)
    merged_columns = []
    for merged_column_name in merged_columns_order:
        column = tables.Column(verbose_name=merged_column_name,
                               attrs={"th": {"rowspan": len(labels)}})
        merged_columns.append(column)
    merged_table.merged_columns = merged_columns

    # Merge data for table
    data = merge_table_dicts(labels, tables_dicts, key_column_name)
    return merged_table(data)

这是模板的一个变化部分:

<thead>
    <tr>
    {% with column=table.columns|first %}
        {% if column.orderable %}
        <th rowspan="2" {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
        {% else %}
        <th rowspan="2" {{ column.attrs.th.as_html }}>{{ column.header }}</th>
        {% endif %}
    {% endwith %}
    {% for column in table.merged_columns %}
        <th colspan="{{ column.attrs.th.rowspan }}">{{ column.header }}</th>
    {% endfor %}
    </tr>
    <tr>
    {% for column in table.columns %}
        {% if not forloop.first %}
            {% if column.orderable %}
            <td {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></td>
            {% else %}
            <td {{ column.attrs.th.as_html }}>{{ column.header }}</td>
            {% endif %}
        {% endif %}
    {% endfor %}
    </tr>
</thead>