File: column-headers-and-footers.rst

package info (click to toggle)
django-tables 2.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,752 kB
  • sloc: python: 7,120; makefile: 132; sh: 74
file content (153 lines) | stat: -rw-r--r-- 5,157 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
.. _column-headers-and-footers:

Customizing headers and footers
===============================

By default an header and no footer will be rendered.


Adding column headers
---------------------

The header cell for each column comes from `~.Column.header`. By default this
method returns `~.Column.verbose_name`, falling back to the capitalized attribute
name of the column in the table class.

When using QuerySet data and a verbose name has not been explicitly defined for
a column, the corresponding model field's `verbose_name` will be used.

Consider the following:

    >>> class Region(models.Model):
    ...     name = models.CharField(max_length=200)
    ...
    >>> class Person(models.Model):
    ...     first_name = models.CharField(verbose_name="model verbose name", max_length=200)
    ...     last_name = models.CharField(max_length=200)
    ...     region = models.ForeignKey('Region')
    ...
    >>> class PersonTable(tables.Table):
    ...     first_name = tables.Column()
    ...     ln = tables.Column(accessor="last_name")
    ...     region_name = tables.Column(accessor="region__name")
    ...
    >>> table = PersonTable(Person.objects.all())
    >>> table.columns["first_name"].header
    'Model Verbose Name'
    >>> table.columns["ln"].header
    'Last Name'
    >>> table.columns["region_name"].header
    'Name'

As you can see in the last example (region name), the results are not always
desirable when an accessor is used to cross relationships. To get around this
be careful to define `.Column.verbose_name`.

.. _ordering-class-name:

Changing class names for ordered column headers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When a column is ordered in an ascending state there needs to be a way to show
it in the interface. django-tables2 does this by adding an `asc` class for
ascending or a `desc` class for descending. It should also be known that any
orderable column is added with an `orderable` class to the column header.

Sometimes there may be a need to change these default classes.

On the `attrs` attribute of the table, you can add a `th` key with the value of
a dictionary. Within that `th` dictionary, you may add an `_ordering` key also
with the value of a dictionary.

The `_ordering` element is optional and all elements within it are optional.
Inside you can have an `orderable` element, which will change the default
`orderable` class name. You can also have `ascending` which will will change the
default `asc` class name. And lastly, you can have `descending` which will
change the default `desc` class name.

Example::

    class Table(tables.Table):
        Meta:
            attrs = {
                "th" : {
                    "_ordering": {
                        "orderable": "sortable", # Instead of `orderable`
                        "ascending": "ascend",   # Instead of `asc`
                        "descending": "descend"  # Instead of `desc`
                    }
                }
            }


It can also be specified at initialization using the `attrs` for both: table and
column::

    ATTRIBUTES = {
        "th" : {
            "_ordering": {
                "orderable": "sortable", # Instead of `orderable`
                "ascending": "ascend",   # Instead of `asc`
                "descending": "descend"  # Instead of `desc`
            }
        }
    }

    table = tables.Table(queryset, attrs=ATTRIBUTES)

    # or

    class Table(tables.Table):
        my_column = tables.Column(attrs=ATTRIBUTES)


.. _column-footers:

Adding column footers
---------------------

By default, no footer will be rendered. If you want to add a footer, define a
footer on at least one column.

That will make the table render a footer on every view of the table. It is up to
you to decide if that makes sense if your table is paginated.

Pass `footer`-argument to the `~.Column` constructor.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The simplest case is just passing a `str` as the footer argument to a column::

    country = tables.Column(footer="Total:")

This will just render the string in the footer. If you need to do more complex
things, like showing a sum or an average, you can pass a callable::

    population = tables.Column(
        footer=lambda table: sum(x["population"] for x in table.data)
    )

You can expect `table`, `column` and `bound_column` as argument.

Define `render_footer` on a custom column.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you need the same footer in multiple columns, you can create your own custom
column. For example this column that renders the sum of the values in the column::

    class SummingColumn(tables.Column):
        def render_footer(self, bound_column, table):
            return sum(bound_column.accessor.resolve(row) for row in table.data)


Then use this column like so::

    class Table(tables.Table):
        name = tables.Column()
        country = tables.Column(footer="Total:")
        population = SummingColumn()


.. note::

    If you are summing over tables with big datasets, chances are it is going
    to be slow. You should use some database aggregation function instead.