--- /dev/null
+table.babyblue {
+ border-collapse: collapse;
+ border-color: #CCC;
+ border: 1px solid #DDD;
+ font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
+}
+
+table.babyblue a:link,
+table.babyblue a:visited {
+ color: #5B80B2;
+ text-decoration: none;
+ font-weight: bold;
+}
+
+table.babyblue a:hover {
+ color: #036;
+}
+
+table.babyblue td,
+table.babyblue th {
+ padding: 5px;
+ line-height: 13px;
+ border-bottom: 1px solid #EEE;
+ border-left: 1px solid #DDD;
+}
+
+table.babyblue thead th:first-child,
+table.babyblue thead td:first-child {
+ border-left: none !important;
+}
+
+table.babyblue thead th,
+table.babyblue thead td {
+ background: #FCFCFC url(../img/nav-bg.gif) top left repeat-x;
+ border-bottom: 1px solid #DDD;
+ padding: 2px 5px;
+ font-size: 11px;
+ vertical-align: middle;
+ white-space: nowrap;
+ color: #666;
+}
+
+table.babyblue thead th > a:link,
+table.babyblue thead th > a:visited {
+ color: #666;
+ display: block;
+}
+
+table.babyblue tr.odd {
+ background-color: #EDF3FE;
+}
+
+table.babyblue tr.even {
+ background-color: white;
+}
from django.template.loader import get_template
from django.template import Context
from django.utils.encoding import StrAndUnicode
-from .utils import rmprefix, toggleprefix, OrderByTuple, Accessor
+from .utils import (rmprefix, toggleprefix, OrderByTuple, Accessor,
+ AttributeDict)
from .columns import Column
from .rows import Rows, BoundRow
from .columns import Columns
class TableOptions(object):
+ """Options for a :term:`table`.
+
+ The following parameters are extracted via attribute access from the
+ *object* parameter.
+
+ :param sortable:
+ bool determining if the table supports sorting.
+ :param order_by:
+ tuple describing the fields used to order the contents.
+ :param attrs:
+ HTML attributes added to the ``<table>`` tag.
+
+ """
def __init__(self, options=None):
super(TableOptions, self).__init__()
self.sortable = getattr(options, 'sortable', None)
self.order_by = getattr(options, 'order_by', ())
+ self.attrs = AttributeDict(getattr(options, 'attrs', {}))
class Table(StrAndUnicode):
features require a RequestContext. Use the ``render_table`` template
tag (requires ``{% load django_tables %}``) if you require this extra
functionality.
+
"""
template = get_template('django_tables/basic_table.html')
return template.render(Context({'table': self}))
+ @property
+ def attrs(self):
+ """The attributes that should be applied to the ``<table>`` tag when
+ rendering HTML.
+
+ ``attrs`` is an :class:`AttributeDict` object which allows the
+ attributes to be rendered to HTML element style syntax via the
+ :meth:`~AttributeDict.as_html` method.
+
+ """
+ return self._meta.attrs
+
def paginate(self, klass=Paginator, page=1, *args, **kwargs):
self.paginator = klass(self.rows, *args, **kwargs)
try:
{% spaceless %}
-<table>
+<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
<thead>
- <tr>
+ <tr class="{% cycle "odd" "even" %}">
{% for column in table.columns %}
<th>{{ column }}</th>
{% endfor %}
</thead>
<tbody>
{% for row in table.rows %}
- <tr>
+ <tr class="{% cycle "odd" "even" %}">
{% for value in row %}
<td>{{ value }}</td>
{% endfor %}
{% load django_tables %}
{% spaceless %}
-<table>
+<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
<thead>
- <tr>
+ <tr class="{% cycle "odd" "even" %}">
{% for column in table.columns %}
<th><a href="{% set_url_param sort=column.name_toggled %}">{{ column }}</a></th>
{% endfor %}
</thead>
<tbody>
{% for row in table.rows %}
- <tr>
+ <tr class="{% cycle "odd" "even" %}">
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
from django.utils.datastructures import SortedDict
from django.template import Context
from django.utils.encoding import force_unicode, StrAndUnicode
+from django.utils.safestring import mark_safe
+from django.template.defaultfilters import escape
__all__ = ('BaseTable', 'options')
@property
def bits(self):
return self.path.split(self.SEPARATOR)
+
+
+class AttributeDict(dict):
+ """A wrapper around :class:`dict` that knows how to render itself as HTML
+ style tag attributes.
+
+ """
+ def as_html(self):
+ """Render as HTML style tag attributes."""
+ return mark_safe(' '.join(['%s="%s"' % (k, escape(v))
+ for k, v in self.iteritems()]))
rendered. Each approach provides balance of ease-of-use and control (the more
control you want, the less easy it is to use).
+CSS
+---
+
+If you want to affect the appearance of the table using CSS, you probably want
+to add a ``class`` or ``id`` attribute to the ``<table>`` element. This can be
+achieved by specifying an ``attrs`` variable in the table's ``Meta`` class.
+
+.. code-block:: python
+
+ >>> import django_tables as tables
+ >>> class SimpleTable(tables.Table):
+ ... id = tables.Column()
+ ... age = tables.Column()
+ ...
+ ... class Meta:
+ ... attrs = {'class': 'mytable'}
+ ...
+ >>> table = SimpleTable()
+ >>> table.as_html()
+ '<table class="mytable">...'
+
+The :attr:`Table.attrs` property actually returns an :class:`AttributeDict`
+object. These objects are identical to :class:`dict`, but have an
+:meth:`AttributeDict.as_html` method that returns a HTML tag attribute string.
+
+.. code-block:: python
+
+ >>> from django_tables.utils import AttributeDict
+ >>> attrs = AttributeDict({'class': 'mytable', 'id': 'someid'})
+ >>> attrs.as_html()
+ 'class="mytable" id="someid"'
+
+The returned string is marked safe, so it can be used safely in a template.
Column formatter
----------------
------------------------
.. autoclass:: django_tables.tables.Table
- :members: __init__, data, order_by, rows, columns, as_html, paginate
+ :members:
+
+
+:class:`TableOptions` Objects:
+------------------------------
+
+.. autoclass:: django_tables.tables.TableOptions
+ :members:
:class:`Column` Objects:
:members: __init__, __getitem__, __contains__, __iter__, record, table
+:class:`AttributeDict` Objects
+------------------------------
+
+.. autoclass:: django_tables.utils.AttributeDict
+ :members:
+
+
Glossary
========