Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6eaeb57

Browse files
committed
Added documentation for the new "render_COLUMN" methods.
1 parent d79158e commit 6eaeb57

File tree

2 files changed

+49
-8
lines changed

2 files changed

+49
-8
lines changed

docs/columns.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ The overwritten ``book_name`` field/column will now be exposed as the
5050
cleaner ``name``, and the new ``author`` column retrieves it's values from
5151
``Book.info.author.name``.
5252

53-
Note: ``data`` may also be a callable which will be passed a row object.
54-
5553
Apart from their internal name, you can define a string that will be used
5654
when for display via ``verbose_name``:
5755

docs/templates.rst

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
================================
2-
Rendering the table in templates
3-
================================
4-
1+
===================
2+
Rendering the table
3+
===================
54

65
A table instance bound to data has two attributes ``columns`` and ``rows``,
76
which can be iterate over:
@@ -27,11 +26,55 @@ For the attributes available on a bound column, see :doc:`features/index`,
2726
depending on what you want to accomplish.
2827

2928

29+
Custom render methods
30+
---------------------
31+
32+
Often, displaying a raw value of a table cell is not good enough. For
33+
example, if your table has a ``rating`` column, you might want to show
34+
an image showing the given number of **stars**, rather than the plain
35+
numeric value.
36+
37+
While you can always write your templates so that the column in question
38+
is treated separately, either by conditionally checking for a column name,
39+
or by explicitely rendering each column manually (as opposed to simply
40+
looping over the ``rows`` and ``columns`` attributes), this is often
41+
tedious to do.
42+
43+
Instead, you can opt to move certain formatting responsibilites into
44+
your Python code:
45+
46+
.. code-block:: django
47+
48+
class BookTable(tables.ModelTable):
49+
name = tables.Column()
50+
rating_int = tables.Column(name="rating")
51+
52+
def render_rating(self, instance):
53+
if instance.rating_count == 0:
54+
return '<img ="no-rating.png">'
55+
else:
56+
return '<img ="rating-%s.png">' % instance.rating_int
57+
58+
When accessing ``table.rows[i].rating``, the ``render_rating`` method
59+
will be called. Note the following:
60+
61+
- What is passed is underlying raw data object, in this case, the
62+
model instance. This gives you access to data values that may not
63+
have been defined as a column.
64+
- For the method name, the public name of the column must be used, not
65+
the internal field name. That is, it's ``render_rating``, not
66+
``render_rating_int``.
67+
- The method is called whenever the cell value is retrieved by you,
68+
whether from Python code or within templates. However, operations by
69+
``django-tables``, like sorting, always work with the raw data.
70+
71+
3072
The table.columns container
3173
---------------------------
3274

33-
While you can iterate through ``columns`` and get all the currently visible
34-
columns, it further provides features that go beyond a simple iterator.
75+
While you can iterate through the ``columns`` attribute and get all the
76+
currently visible columns, it further provides features that go beyond
77+
a simple iterator.
3578

3679
You can access all columns, regardless of their visibility, through
3780
``columns.all``.

0 commit comments

Comments
 (0)