added SimpleModel for TableWidget stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Thu, 05 Nov 2009 12:41:58 +0100
branchstable
changeset 3796 f4e924106fdc
parent 3795 ce6381416a12
child 3797 af9b0e113108
added SimpleModel for TableWidget
web/htmlwidgets.py
--- a/web/htmlwidgets.py	Thu Nov 05 12:41:03 2009 +0100
+++ b/web/htmlwidgets.py	Thu Nov 05 12:41:58 2009 +0100
@@ -12,7 +12,8 @@
 from logilab.mtconverter import xml_escape
 
 from cubicweb.utils import UStringIO
-from cubicweb.common.uilib import toggle_action
+from cubicweb.common.uilib import toggle_action, limitsize, htmlescape
+from cubicweb.web import jsonize
 
 # XXX HTMLWidgets should have access to req (for datadir / static urls,
 #     i18n strings, etc.)
@@ -250,6 +251,36 @@
     def add_attr(self, attr, value):
         self.cell_attrs[attr] = value
 
+class SimpleTableModel(object):
+    """
+    uses a list of lists as a storage backend
+
+    NB: the model expectes the cellvid passed to
+    TableColumn.append_renderer to be a callable accepting a single
+    argument and returning a unicode object
+    """
+    def __init__(self, rows):
+        self._rows = rows
+
+
+    def get_rows(self):
+        return self._rows
+
+    def render_cell(self, cellvid, rowindex, colindex, w):
+        value = self._rows[rowindex][colindex]
+        w(cellvid(value))
+
+    @htmlescape
+    @jsonize
+    def sortvalue(self, rowindex, colindex):
+        value =  self._rows[rowindex][colindex]
+        if value is None:
+            return u''
+        elif isinstance(value, int):
+            return u'%09d'%value
+        else:
+            return unicode(value)
+
 
 class TableWidget(HTMLWidget):