From 285ed9f43d1e41d1521a826ef895ba7c236211b5 Mon Sep 17 00:00:00 2001 From: Bradley Ayers Date: Fri, 25 Mar 2011 12:51:35 +1000 Subject: [PATCH] * Added 'attrs' property on Table * Added 'attrs' field in Table.Meta * Added AttributeDict * Updated documentation relating to the new Table.attrs property * Added a paleblue theme. --- .../themes/paleblue/css/screen.css | 55 ++++++++++++++++++ .../themes/paleblue/img/nav-bg.gif | Bin 0 -> 273 bytes django_tables/tables.py | 30 +++++++++- .../templates/django_tables/basic_table.html | 6 +- .../templates/django_tables/table.html | 6 +- django_tables/utils.py | 13 +++++ docs/conf.py | 4 +- docs/index.rst | 50 +++++++++++++++- setup.py | 2 +- 9 files changed, 155 insertions(+), 11 deletions(-) create mode 100644 django_tables/static/django_tables/themes/paleblue/css/screen.css create mode 100644 django_tables/static/django_tables/themes/paleblue/img/nav-bg.gif 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 0000000000000000000000000000000000000000..f8402b809dc1efec80db6e466d789f88429a79a8 GIT binary patch literal 273 zcmZ?wbhEHb6l9QRIKsg2{rmT~Z{I$D{`}ReSAYNh{q^hDhYue(@t*9{u_A=gpfpKY#xG_U+rVXV3oq`}hC||i=Q&8(m$()z5YF*CjUEw001<4;4tjrTwH+&%FA_x~E6rskH`CdQ7= zuI>)uzWxal`(>Zw+Pr1!Chi?O LckSMx$Y2cs9wnNk literal 0 HcmV?d00001 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/conf.py b/docs/conf.py index 93ad764..ada3098 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -50,9 +50,9 @@ project = u'django-tables' # built documents. # # The short X.Y version. -version = '0.4.0.alpha2' +version = '0.4.0.alpha3' # The full version, including alpha/beta/rc tags. -release = '0.4.0.alpha2' +release = '0.4.0.alpha3' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. diff --git a/docs/index.rst b/docs/index.rst index 7cbe396..6aa6d1c 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 ---------------- @@ -173,6 +206,7 @@ If formatters aren't powerful enough, you'll need to either :ref:`create a Column subclass `, or to use the :ref:`Table.render_FOO method `. +.. _table.render_foo: .. _table.render_foo: @@ -403,7 +437,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 +484,13 @@ API Reference :members: __init__, __getitem__, __contains__, __iter__, record, table +:class:`AttributeDict` Objects +------------------------------ + +.. autoclass:: django_tables.utils.AttributeDict + :members: + + Glossary ======== diff --git a/setup.py b/setup.py index 3b2c8cf..10b2497 100755 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages setup( name='django-tables', - version='0.4.0.alpha2', + version='0.4.0.alpha3', description='Table framework for Django', author='Bradley Ayers', -- 2.26.2