Anonymous avatar Anonymous committed 66be514

Change ``ListGrid`` constructor args.

Comments (0)

Files changed (3)

 tip (development version)
+* webhelpers.html.grid  / webhelpers.pylonslib.grid:
+ - A new ``ListGrid`` class to turn a list of lists into a table.
+ - Bugfixes by Ergo.
 * webhelpers.misc:
  - New ``format_exception`` helper to display an exception as Python would but


     """ A grid class for a sequence of lists.
     This grid class assumes that the rows are lists rather than dicts, and
-    uses attribute access to retrieve the column values.
-    The columns attribute during init MUST specify list indices as strings 
-    like this: grid = ListGrid(list_data, columns=['1', '3', '2', '0']) 
+    uses subscript access to retrieve the column values. Some constructor args
+    are also different.
+    If ``columns`` is not specified in the constructor, it will examine 
+    ``itemlist[0]`` to determine the number of columns, and display them in
+    order.  This works only if ``itemlist`` is a sequence and not just an
+    iterable.  Alternatively, you can pass an int to specify the number of
+    columns, or a list of int subscripts to override the column order.
+    Examples::
+        grid = ListGrid(list_data)
+        grid = ListGrid(list_data, columns=4)
+        grid = ListGrid(list_data, columns=[1, 3, 2, 0]) 
+    ``column_labels`` may be a list of strings. The class will calculate the
+    appropriate subscripts for the superclass dict.
+    def __init__(self, itemlist, columns=None, column_labels=None, *args, **kw):
+        if columns is None:
+            columns = range(len(itemlist[0]))
+        elif isinstance(columns, int):
+            columns = range(columns)
+        # The superclass requires the ``columns`` elements to be strings.
+        super_columns = [str(x) for x in columns]
+        # The superclass requires ``column_labels`` to be a dict.
+        super_labels = column_labels
+        if isinstance(column_labels, (list, tuple)):
+            super_labels = dict(zip(super_columns, column_labels))
+        Grid.__init__(self, itemlist, super_columns, super_labels, *args, **kw)
     def default_column_format(self, column_number, i, record, column_name):
         class_name = "c%s" % (column_number)
         return HTML.tag("td", record[int(column_name)], class_=class_name)


         basic demo
-        g = ListGrid(list_data, columns=['1', '3', '2', '0'])
+        g = ListGrid(list_data, columns=[1, 3, 2, 0],
+            column_labels=["One", "Three", "Two", "Zero"])
         return g
 demos = subclasses_only(_DemoBase, globals())
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
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.