From: Bradley Ayers Date: Fri, 25 Mar 2011 02:49:41 +0000 (+1000) Subject: added 'attrs' field on Table.Meta, updated documentation, added a paleblue theme. X-Git-Tag: v0.4.0.alpha4^2~3 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=c743e17d0405b5b362fc0676a51b69eb76dbe0d4;p=django-tables2.git added 'attrs' field on Table.Meta, updated documentation, added a paleblue theme. --- diff --git a/django_tables/static/django_tables/themes/paleblue/css/screen.css b/django_tables/static/django_tables/themes/paleblue/css/screen.css new file mode 100644 index 0000000..771a49f --- /dev/null +++ b/django_tables/static/django_tables/themes/paleblue/css/screen.css @@ -0,0 +1,55 @@ +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; +} diff --git a/django_tables/static/django_tables/themes/paleblue/img/nav-bg.gif b/django_tables/static/django_tables/themes/paleblue/img/nav-bg.gif new file mode 100644 index 0000000..f8402b8 Binary files /dev/null and b/django_tables/static/django_tables/themes/paleblue/img/nav-bg.gif differ diff --git a/django_tables/tables.py b/django_tables/tables.py index 2897e50..2a3cebb 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -6,7 +6,8 @@ from django.http import Http404 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 @@ -164,10 +165,24 @@ class DeclarativeColumnsMetaclass(type): 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 ```` 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): @@ -267,10 +282,23 @@ 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 ``
`` 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: diff --git a/django_tables/templates/django_tables/basic_table.html b/django_tables/templates/django_tables/basic_table.html index 50fb701..323ce3e 100644 --- a/django_tables/templates/django_tables/basic_table.html +++ b/django_tables/templates/django_tables/basic_table.html @@ -1,7 +1,7 @@ {% spaceless %} -
+ - + {% for column in table.columns %} {% endfor %} @@ -9,7 +9,7 @@ {% for row in table.rows %} - + {% for value in row %} {% endfor %} diff --git a/django_tables/templates/django_tables/table.html b/django_tables/templates/django_tables/table.html index 27ecfd6..c375ce6 100644 --- a/django_tables/templates/django_tables/table.html +++ b/django_tables/templates/django_tables/table.html @@ -1,8 +1,8 @@ {% load django_tables %} {% spaceless %} -
{{ column }}
{{ value }}
+ - + {% for column in table.columns %} {% endfor %} @@ -10,7 +10,7 @@ {% for row in table.rows %} - + {% for cell in row %} {% endfor %} diff --git a/django_tables/utils.py b/django_tables/utils.py index ac052b6..5f19062 100644 --- a/django_tables/utils.py +++ b/django_tables/utils.py @@ -2,6 +2,8 @@ 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') @@ -141,3 +143,14 @@ class Accessor(object): @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()])) diff --git a/docs/index.rst b/docs/index.rst index 7cbe396..96ec148 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -138,6 +138,39 @@ There are a number of options available for changing the way the table is 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 ``
{{ column }}
{{ cell }}
`` 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() + '
...' + +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 ---------------- @@ -403,7 +436,14 @@ API Reference ------------------------ .. 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: @@ -443,6 +483,13 @@ API Reference :members: __init__, __getitem__, __contains__, __iter__, record, table +:class:`AttributeDict` Objects +------------------------------ + +.. autoclass:: django_tables.utils.AttributeDict + :members: + + Glossary ========