Allow setting of DataTables column type
authorThierry Florac <thierry.florac@onf.fr>
Wed, 07 Oct 2015 18:02:54 +0200
changeset 95 cfdcf86462f5
parent 94 229b0844c578
child 96 7b6902a80d29
Allow setting of DataTables column type
src/pyams_skin/table.py
--- a/src/pyams_skin/table.py	Wed Oct 07 18:01:32 2015 +0200
+++ b/src/pyams_skin/table.py	Wed Oct 07 18:02:54 2015 +0200
@@ -18,6 +18,7 @@
 # import interfaces
 from pyams_skin.interfaces.container import ITable, ITableElementEditor, ITableWithActions, ITableElementName
 from pyams_skin.layer import IPyAMSLayer
+from pyams_utils.interfaces import MANAGE_PERMISSION
 from pyams_viewlet.interfaces import IViewletManager
 from z3c.table.interfaces import IColumn, IValues
 from zope.container.interfaces import IContained, IContainer
@@ -65,11 +66,16 @@
 
     @property
     def data_attributes(self):
-        return {'tr': {'id': lambda x: '{0}::{1}'.format(self.id, getattr(x, '__name__', '')),
-                       'data-ams-element-name': lambda x: getattr(x, '__name__', None),
-                       'data-ams-url': lambda x: getattr(get_element_editor(self, x), 'url', None),
-                       'data-toggle': lambda x: 'modal' if getattr(get_element_editor(self, x), 'modal_target', None)
-                                                else None}}
+        return {'tr': {'id':
+                           lambda x, col: '{0}::{1}'.format(self.id, getattr(x, '__name__', '')),
+                       'data-ams-element-name':
+                           lambda x, col: getattr(x, '__name__', None),
+                       'data-ams-url':
+                           lambda x, col: getattr(get_element_editor(self, x), 'url', None),
+                       'data-toggle':
+                           lambda x, col: 'modal' if getattr(get_element_editor(self, x), 'modal_target', None)
+                                          else None},
+                'th': {'data-ams-datatable-stype': self.get_column_type}}
 
     batchSize = 10000
     startBatchingAt = 10000
@@ -87,9 +93,13 @@
         return self.request.params.get(self.prefix + '-sortOrder', self.sortOrder)
 
     @staticmethod
-    def check_data_attribute(attribute, source):
+    def get_column_type(column, _=None):
+        return getattr(column, 'dt_sort_type', None)
+
+    @staticmethod
+    def check_data_attribute(attribute, source, column=None):
         if callable(attribute):
-            return attribute(source)
+            return attribute(source, column)
         else:
             return str(attribute)
 
@@ -98,7 +108,7 @@
         if attrs:
             result = ''
             for key, value in attrs.items():
-                checked_value = self.check_data_attribute(value, source)
+                checked_value = self.check_data_attribute(value, source, column)
                 if checked_value is not None:
                     result += ' {0}="{1}"'.format(key, checked_value)
             return result
@@ -115,6 +125,10 @@
         return super(BaseTable, self).renderRow(row, cssClass) \
                                      .replace('<tr', '<tr %s' % self.get_data_attributes('tr', row[0][0]))
 
+    def renderHeadCell(self, column):
+        return super(BaseTable, self).renderHeadCell(column) \
+                                     .replace('<th', '<th %s' % self.get_data_attributes('th', column))
+
     def renderCell(self, item, column, colspan=0):
         return super(BaseTable, self).renderCell(item, column, colspan) \
                                      .replace('<td', '<td %s' % self.get_data_attributes('td', item, column))
@@ -245,7 +259,7 @@
     url = 'MyAMS.container.deleteElement'
     icon_class = 'fa fa-fw fa-trash'
     icon_hint = _("Delete object")
-    permission = 'manage'
+    permission = MANAGE_PERMISSION
     weight = 9999
 
     def get_url(self, item):