Mike Orr avatar Mike Orr committed a062678 Merge

Merge.

Comments (0)

Files changed (1)

webhelpers/html/grid.py

     """
     def __init__(self, itemlist, columns, column_labels=None,
                   column_formats=None, start_number=1,
-                 order_column=None, order_direction=None):
+                 order_column=None, order_direction=None, request=None,
+                 url_generator=None, **kw):
+        """ additional keywords are appended to self.additional_kw 
+        handy for url generation """
         self.labels = column_labels or {}
         self.exclude_ordering = columns
         self.itemlist = itemlist
         self.start_number = start_number
         self.order_dir = order_direction
         self.order_column = order_column
+        #bw compat with old pylons grid
+        if not hasattr(self,'request'):
+            self.request = request
+        self.url_generator = url_generator
+        self.additional_kw = kw
     
     def calc_row_no(self, i, column):
         if self.order_dir == 'dsc' and self.order_column == column:
         return self.__html__()
 
     def generate_header_link(self, column_number, column, label_text):
+        """ This handles generation of link and then decides to call
+        self.default_header_ordered_column_format 
+        or 
+        self.default_header_column_format 
+        based on if current column is the one that is used for sorting or not
+        
+        you need to extend Grid class and overload this method implementing
+        ordering here, whole operation consists of setting
+        self.order_column and self.order_dir to their CURRENT values,
+        and generating new urls for state that header should set set after its
+        clicked
+        
+        (additional kw are passed to url gen. - like for webhelpers.paginate)
+        example URL generation code below:
+        
+        GET = dict(self.request.copy().GET) # needs dict() for py2.5 compat
+        self.order_column = GET.pop("order_col", None)
+        self.order_dir = GET.pop("order_dir", None)       
+        # determine new order
+        if column == self.order_column and self.order_dir == "asc":
+            new_order_dir = "dsc"
+        else:
+            new_order_dir = "asc"
+        self.additional_kw['order_col'] = column
+        self.additional_kw['order_dir'] = new_order_dir  
+        # generate new url for example url_generator uses 
+        # pylons's url.current() or pyramid's current_route_url()
+        new_url = self.url_generator(**self.additional_kw)
+        # set label for header with link
+        label_text = HTML.tag("a", href=new_url, c=label_text)
+        """ 
+        
         # Is the current column the one we're ordering on?
         if (column == self.order_column):
             return self.default_header_ordered_column_format(column_number,
                                                              label_text)
         else:
             return self.default_header_column_format(column_number, column,
-                                                     label_text)
+                                                     label_text)            
 
     #### Default HTML tag formats ####
 
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.