From: Michael Elsdoerfer Date: Sun, 29 Jun 2008 10:30:46 +0000 (+0200) Subject: added columns.sortable() method X-Git-Tag: 0.2~50 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d9801dbf9b0d17b0a2f59a64fee667b7d56e10c9;p=django-tables2.git added columns.sortable() method --- diff --git a/README b/README index 485e87d..4467b37 100644 --- a/README +++ b/README @@ -308,6 +308,20 @@ Note that how the default is used and when it is applied differs between static and ModelTables. +The table.columns container +--------------------------- + +While you can iterate through ``columns`` and get all the currently visible +columns, it further provides features that go beyond a simple iterator. + +You can access all columns, regardless of their visibility, through +``columns.all``. + +``columns.sortable`` is a handy shortcut that exposes all columns which's +``sortable`` attribute is True. This can be very useful in templates, when +doing {% if column.sortable %} can conflict with {{ forloop.last }}. + + Tables and Pagination --------------------- diff --git a/django_tables/tables.py b/django_tables/tables.py index e6c09ed..7a284a0 100644 --- a/django_tables/tables.py +++ b/django_tables/tables.py @@ -421,6 +421,18 @@ class Columns(object): self._spawn_columns() return self._columns.keyOrder.index(name) + def sortable(self): + """Iterate through all sortable columns. + + This is primarily useful in templates, where iterating over the full + set and checking {% if column.sortable %} can be problematic in + conjunction with e.g. {{ forloop.last }} (the last column might not + be the actual last that is rendered). + """ + for column in self.all(): + if column.sortable: + yield column + def __iter__(self): """Iterate through all *visible* bound columns. diff --git a/tests/test_basic.py b/tests/test_basic.py index b0dde2f..589bbfc 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -267,10 +267,24 @@ def test_pagination(): assert books.page.has_previous() == False assert books.page.has_next() == True +# TODO: all the column stuff might warrant it's own test file def test_columns(): - """Test specific column features. + """Test Table.columns container functionality. + """ + + class BookTable(tables.Table): + id = tables.Column(sortable=False) + name = tables.Column(sortable=True) + pages = tables.Column(sortable=True) + language = tables.Column(sortable=False) + books = BookTable([]) - Might warrant it's own test file.""" + assert list(books.columns.sortable()) == [c for c in books.columns if c.sortable] + + +def test_column_order(): + """Test the order functionality of bound columns. + """ class BookTable(tables.Table): id = tables.Column()