[Checkins] SVN: z3c.table/branches/darrylcousins/src/z3c/table/ Put a generic table column header sorting class in the package, and added test in README. The column header is registered as a multi-adapter to the column.

Darryl Cousins darryl at darrylcousins.net.nz
Thu Mar 13 00:08:51 EDT 2008


Log message for revision 84630:
  Put a generic table column header sorting class in the package, and added test in README. The column header is registered as a multi-adapter to the column.

Changed:
  U   z3c.table/branches/darrylcousins/src/z3c/table/README.txt
  U   z3c.table/branches/darrylcousins/src/z3c/table/column.py

-=-
Modified: z3c.table/branches/darrylcousins/src/z3c/table/README.txt
===================================================================
--- z3c.table/branches/darrylcousins/src/z3c/table/README.txt	2008-03-13 03:43:37 UTC (rev 84629)
+++ z3c.table/branches/darrylcousins/src/z3c/table/README.txt	2008-03-13 04:08:50 UTC (rev 84630)
@@ -1595,47 +1595,52 @@
 registering a IHeaderColumn adapter. This may be useful for adding links to
 column headers for an existing table implementation.
 
-  >>> class TitleColumnHeader(column.ColumnHeader):
+We'll use a fresh almost empty container.
+
+  >>> container = Container()
+  >>> root['container-1'] = container
+  >>> container[u'first'] = Content('First', 1)
+  >>> container[u'second'] = Content('Second', 2)
+  >>> container[u'third'] = Content('Third', 3)
+
+  >>> class myTableClass(table.Table):
+  ...     pass
+
+  >>> myTable = myTableClass(container, request)
+
+  >>> class TitleColumn(column.Column):
   ... 
-  ...     def update(self):
-  ...         pass
+  ...     header = u'Title'
   ... 
-  ...     def render(self):
-  ...         return u'<span>'+self.column.header+'</span>'
+  ...     def renderCell(self, item):
+  ...         return item.title
 
-  >>> zope.component.provideAdapter(TitleColumnHeader,
-  ...     (None, None, NumberColumn), provides=interfaces.IColumnHeader)
+Now we can register a column adapter directly to our table class.
 
-  >>> print sequenceTable.render()
+  >>> zope.component.provideAdapter(TitleColumn,
+  ...     (None, None, myTableClass), provides=interfaces.IColumn,
+  ...      name='titleColumn')
+
+And add a registration for a column header - we'll use here the proveded generic
+sorting header implementation.
+
+  >>> from z3c.table.column import SortingColumnHeader
+  >>> zope.component.provideAdapter(SortingColumnHeader,
+  ...     (None, None, interfaces.IColumn), provides=interfaces.IColumnHeader)
+
+Now we can render the table and we shall see a link in the header. Note that it
+is set to switch to descending as the the table initially will display the first
+column as ascending.
+
+  >>> myTable.update()
+  >>> print myTable.render()
   <table>
-    <thead>
-      <tr>
-        <th>My items</th>
-        <th><span>Number</span></th>
-      </tr>
-    </thead>
-    <tbody>
-      <tr>
-        <td>Twentieth item</td>
-        <td>number: 20</td>
-      </tr>
-      <tr>
-        <td>Nineteenth item</td>
-        <td>number: 19</td>
-      </tr>
-      <tr>
-        <td>Eighteenth item</td>
-        <td>number: 18</td>
-      </tr>
-      <tr>
-        <td>Seventeenth item</td>
-        <td>number: 17</td>
-      </tr>
-      <tr>
-        <td>Sixteenth item</td>
-        <td>number: 16</td>
-      </tr>
-    </tbody>
+   <thead>
+    <tr>
+     <th><a
+      href="?table-sortOrder=descending&table-sortOn=table-titleColumn-0"
+      title="Sort">Title</a></th>
+  ...
   </table>
 
 Miscellaneous

Modified: z3c.table/branches/darrylcousins/src/z3c/table/column.py
===================================================================
--- z3c.table/branches/darrylcousins/src/z3c/table/column.py	2008-03-13 03:43:37 UTC (rev 84629)
+++ z3c.table/branches/darrylcousins/src/z3c/table/column.py	2008-03-13 04:08:50 UTC (rev 84630)
@@ -16,6 +16,8 @@
 """
 __docformat__ = "reStructuredText"
 
+from urllib import urlencode
+
 import zope.interface
 import zope.location
 import zope.i18nmessageid
@@ -61,26 +63,7 @@
         return default
 
 
-class ColumnHeader(object):
-    """ColumnHeader renderer provider"""
 
-    zope.interface.implements(interfaces.IColumnHeader)
-
-    def __init__(self, context, request, column):
-        self.__parent__ = context
-        self.context = context
-        self.request = request
-        self.column = column
-
-    def update(self):
-        """Override this method in subclasses if required"""
-        pass
-
-    def render(self):
-        """Override this method in subclasses"""
-        return self.column.header
-
-
 class Column(zope.location.Location):
     """Column provider."""
 
@@ -284,3 +267,58 @@
         if value:
             value = formatter.format(value)
         return value
+
+
+class ColumnHeader(object):
+    """ColumnHeader renderer provider"""
+
+    zope.interface.implements(interfaces.IColumnHeader)
+
+    def __init__(self, context, request, column):
+        self.__parent__ = context
+        self.context = context
+        self.request = request
+        self.column = column
+
+    def update(self):
+        """Override this method in subclasses if required"""
+        pass
+
+    def render(self):
+        """Override this method in subclasses"""
+        return self.column.header
+
+
+class SortingColumnHeader(ColumnHeader):
+    """Sorting column header."""
+
+    def render(self):
+        table = self.column.table
+        prefix = table.prefix
+        colID = self.column.id
+
+        # this may return a string 'id-name-idx' if coming from request,
+        # otherwise in Table class it is intialised as a integer string
+        currentSortID = table.getSortOn()
+        try:
+            currentSortID = int(currentSortID)
+        except ValueError:
+            currentSortID = currentSortID.split('-')[2]
+
+        currentSortOrder = table.getSortOrder()
+
+        sortID = colID.split('-')[2]
+
+        sortOrder = table.sortOrder
+        if int(sortID) == int(currentSortID):
+            # ordering the same column so we want to reverse the order
+            if currentSortOrder == table.sortOrder:
+                sortOrder = table.reverseSortOrderNames[0]
+
+        args = {'%s-sortOn' % prefix: colID,
+                '%s-sortOrder' % prefix: sortOrder}
+        queryString = '?%s' % (urlencode(args))
+
+        return '<a href="%s" title="Sort">%s</a>' % (queryString, 
+                                                self.column.header)
+



More information about the Checkins mailing list