Anonymous avatar Anonymous committed 2922702

initial documentation of grid class - also grid now supports ordering by params that can be passed to instance - to enable markup to reflect ordering. This could be useful for sessions remembering sort order or business logic changing order if specific conditions are met.

Comments (0)

Files changed (3)

webhelpers/html/grid.py

     columns will be rendered - also keep note of special column name that can be
     passed in list that defines order - ``_numbered`` - this adds additional
     column that shows the number of item. For paging sql data there one can pass
-    ``start_number`` argument to the grid to define where to start counting
+    ``start_number`` argument to the grid to define where to start counting.
+    Descendant sorting on ``_numbered`` column decrements the value, you can
+    change how numberign function behaves by overloading ``calc_row_no`` 
+    property.
+    
     
     Then printing the grid instance forces rendering of data into html output,
     the headers are created and for every entry in list a table row will be
      
      
     """
-    def __init__(self, itemlist, columns, column_formats=None, start_number=1,
+    def __init__(self, itemlist, columns, column_labels=None,
+                  column_formats=None, start_number=1,
                  order_column=None, order_direction='asc'):
-        self.custom_record_format = None
-        self.labels = {}
+        self.labels = column_labels or {}
         self.exclude_ordering = columns
         self.itemlist = itemlist
+        self.columns = columns
+        self.column_formats = column_formats or {}
         if "_numbered" in columns:
             self.labels["_numbered"] = "#"
-        self.columns = columns
-        self.column_formats = column_formats or {}
-        self._start_number = start_number
+        if "_numbered" not in self.column_formats: 
+            self.column_formats["_numbered"] = self.numbered_column_format 
+        self.start_number = start_number
         self.order_dir = order_direction
         self.order_column = order_column
     
-    def calc_row_no(self, i, column ):
-        if self.order_dir == 'dsc' and self.order_column == "_numbered":
-            return self._start_number - i
+    def calc_row_no(self, i, column):
+        if self.order_dir == 'dsc' and self.order_column == column:
+            return self.start_number - i
         else:
-            return self._start_number + i
+            return self.start_number + i
         
     def make_headers(self):
         header_columns = []
                 label_text = column.replace("_", " ").title()
             # handle non clickable columns
             if column in self.exclude_ordering:
-                header = self.default_header_column_format(i + 1, column, 
+                header = self.default_header_column_format(i + 1, column,
                     label_text)
             # handle clickable columns
             else:
         columns = []        
         for col_num, column in enumerate(self.columns):
             if column in self.column_formats:
-                columns.append(self.column_formats[column](col_num, i, record))
+                r = self.column_formats[column](col_num,
+                                                self. calc_row_no(i, column),
+                                                record)
             else:
-                if column == "_numbered":
-                    r = self.numbered_column_format(col_num,
-                                                    self.calc_row_no(i, column), 
-                                                    record)
-                else:
-                    r = self.default_column_format(col_num, i, record, column)
-                columns.append(r)
+                r = self.default_column_format(col_num,
+                                               self.calc_row_no(i, column),
+                                               record, column)
+            columns.append(r)
         return HTML(*columns)
     
     def __html__(self):
         # now lets render the actual item grid
         for i, record in enumerate(self.itemlist):
             columns = self.make_columns(i, record)
-            if self.custom_record_format is None:
+            if hasattr(self, 'custom_record_format'):
+                r = self.custom_record_format(i, record, columns)
+            else:
                 r = self.default_record_format(i, record, columns)
-            else:
-                r = self.custom_record_format(i, record, columns)
             records.append(r)
         return HTML(*records)
     
         return self.__html__()
 
     def generate_header_link(self, column_number, column, label_text):
-        if column == self.order_column and self.order_dir == "asc":
-            new_order_dir = "dsc"
-        else:
-            new_order_dir = "asc"
         # Is the current column the one we're ordering on?
         if (column == self.order_column):
             return self.default_header_ordered_column_format(column_number,
-                                                             column, 
+                                                             column,
                                                              label_text)
         else:
             return self.default_header_column_format(column_number, column,
         class_name = "c%s ordering %s %s" % (column_number, self.order_dir, column_name)
         return HTML.tag("td", header_label, class_=class_name)
 
-    def default_header_column_format(self, column_number, column_name, 
+    def default_header_column_format(self, column_number, column_name,
         header_label):
         if column_name == "_numbered":
             column_name = "numbered"

webhelpers/html/grid_demo.py

              {"group_name": "foo4", "options": "lalala4", "id":4},
              ]
 
-#### Demo base class ####
 class _DemoBase(object):
     title = None
     description = None
         
         g = Grid(test_data, columns=["_numbered","group_name","options"])
         g.labels["options"] = 'FOOBAAR'
-        g.column_formats = {
-            "options": options_td,
-            }
+        g.column_formats["options"] = options_td
         return g
 
+class OrderShiftDemo(_DemoBase):
+    name = "OrderShift"
+    description = """\
+This table shows a grid with order starting from 10."""
+
+    def get_grid(self):
+        """
+        order direction demo
+        """
+        
+        g = Grid(test_data, columns=["_numbered","group_name","options"],
+                 start_number=10
+                 )
+        return g
+
+
+class OrderingDirectionHeaderAwareDemo(_DemoBase):
+    name = "OrderDirectionHeaderAwareDemo"
+    description = """\
+This table shows a grid that has a markup indicating order direction.
+Options column has sorting set to "asc" """
+
+    def get_grid(self):
+        """
+        order direction demo
+        """
+        
+        g = Grid(test_data, columns=["_numbered","group_name","options"],
+                 order_column='options', order_direction='asc'
+                 )
+        #enable ordering support
+        g.exclude_ordering = []
+        return g
 demos = subclasses_only(_DemoBase, globals())
 
 #demos = [BasicDemo, CustomColumnDemo]

webhelpers/pylonslib/grid.py

         from pylons import url
         # this will handle possible URL generation
         request_copy = self.request.copy().GET
-        if "order_col" in request_copy and "order_dir" in request_copy:
-            self.order_column = request_copy.pop("order_col")
-            self.order_dir = request_copy.pop("order_dir")
-        else:
-            self.order_column = None
-            self.order_dir = None
-            
+        self.order_column = request_copy.pop("order_col", None)
+        self.order_dir = request_copy.pop("order_dir", None)
         if column == self.order_column and self.order_dir == "asc":
             new_order_dir = "dsc"
         else:
                                **request_copy)
         label_text = HTML.tag("a", href=url_href, c=label_text)
         # Is the current column the one we're ordering on?
-        if (column == self.order_column):
+        if column == self.order_column:
             return self.default_header_ordered_column_format(column_number,
-                                                             column, 
+                                                             column,
                                                              label_text)
         else:
             return self.default_header_column_format(column_number, column,
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.