-
Notifications
You must be signed in to change notification settings - Fork 436
Adds support for header_value
for export header
#970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -338,6 +338,26 @@ def header(self): | |||||||||||
""" | ||||||||||||
return self.verbose_name | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def header_value(self): | ||||||||||||
""" | ||||||||||||
The value used for the column heading in exports. | ||||||||||||
|
||||||||||||
By default this returns `~.Column.header`. | ||||||||||||
|
||||||||||||
:returns: `unicode` or `None` | ||||||||||||
|
||||||||||||
.. note:: | ||||||||||||
|
||||||||||||
This property typically is not accessed directly when a table is | ||||||||||||
rendered. Instead, `.BoundColumn.header_value` is accessed which | ||||||||||||
in turn accesses this property. This allows the header to fallback | ||||||||||||
to the column name (it is only available on a `.BoundColumn` object | ||||||||||||
hence accessing that first) when this property doesn't return something | ||||||||||||
useful. | ||||||||||||
""" | ||||||||||||
return self.header | ||||||||||||
|
||||||||||||
def footer(self, bound_column, table): | ||||||||||||
"""Return the content of the footer, if specified.""" | ||||||||||||
footer_kwargs = {"column": self, "bound_column": bound_column, "table": table} | ||||||||||||
|
@@ -556,6 +576,14 @@ def header(self): | |||||||||||
# fall back to automatic best guess | ||||||||||||
return self.verbose_name | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def header_value(self): | ||||||||||||
"""The contents of the header for this column in an export.""" | ||||||||||||
column_header_value = self.column.header_value | ||||||||||||
if column_header_value: | ||||||||||||
return column_header_value | ||||||||||||
return self.header | ||||||||||||
Comment on lines
+582
to
+585
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can safely write this as:
Suggested change
|
||||||||||||
|
||||||||||||
@property | ||||||||||||
def footer(self): | ||||||||||||
"""The contents of the footer cell for this column.""" | ||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -151,9 +151,9 @@ Please refer to `.Table.as_values` for an example. | |||||
Subclassing `.Column` | ||||||
--------------------- | ||||||
|
||||||
Defining a column subclass allows functionality to be reused across tables. | ||||||
Columns have a `render` method that behaves the same as :ref:`table.render_foo` | ||||||
methods on tables:: | ||||||
Defining a column subclass allows further customization and functionality to | ||||||
be reused across tables. Columns have a `render` method that behaves the same | ||||||
as :ref:`table.render_foo` methods on tables:: | ||||||
|
||||||
>>> import django_tables2 as tables | ||||||
>>> | ||||||
|
@@ -186,3 +186,19 @@ For complicated columns, you may want to return HTML from the | |||||
... def render(self, value): | ||||||
... return format_html('<img src="/media/img/{}.jpg" />', value) | ||||||
... | ||||||
|
||||||
When subclassing a column, the header in the html table can be customized by | ||||||
overriding :meth:`~Column.header`. The header value for exports can be independently | ||||||
customized using :meth:`~Column.render_value`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
|
||||||
>>> from django.utils.html import format_html | ||||||
>>> | ||||||
>>> class PartyColumn(tables.Column): | ||||||
... @property | ||||||
... def header(self): | ||||||
... return format_html('<div><marquee>PaRtY!</marquee></div>') | ||||||
... | ||||||
... @property | ||||||
... def header_value(self): | ||||||
... return "party" |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -80,6 +80,10 @@ By default, it just calls the `render()` method on that column. | |||||
If your custom column produces HTML, you should override this method and return | ||||||
the actual value. | ||||||
|
||||||
For column headers, similarly sometimes :ref:Column.header`-may include html which | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
would be inappropriate to include in an export. For this case, the `:ref:Column.header_value` | ||||||
can be used to override the column header only in exports. | ||||||
|
||||||
|
||||||
Including and excluding columns | ||||||
------------------------------- | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't
unicode
a python 2 construct?