web/views/tableview.py
changeset 5234 e2476d78b060
parent 4618 412a9f1f5fd0
child 5239 471554b842d2
equal deleted inserted replaced
5233:673b63953e7a 5234:e2476d78b060
   324 
   324 
   325 
   325 
   326 class EditableInitialTableTableView(InitialTableView):
   326 class EditableInitialTableTableView(InitialTableView):
   327     __regid__ = 'editable-initialtable'
   327     __regid__ = 'editable-initialtable'
   328     finalview = 'editable-final'
   328     finalview = 'editable-final'
       
   329 
       
   330 
       
   331 class EntityAttributesTableView(EntityView):
       
   332     """This table displays entity attributes in a table and allow to set a
       
   333     specific method to help building cell content for each attribute as well as
       
   334     column header.
       
   335 
       
   336     Table will render entity cell by using the appropriate build_COLNAME_cell
       
   337     methods if defined otherwise cell content will be entity.COLNAME.
       
   338 
       
   339     Table will render column header using the method header_for_COLNAME if
       
   340     defined otherwise COLNAME will be used.
       
   341     """
       
   342     __abstract__ = True
       
   343     columns = ()
       
   344     table_css = "listing"
       
   345     css_files = ()
       
   346 
       
   347     def call(self, columns=None):
       
   348         if self.css_files:
       
   349             self._cw.add_css(self.css_files)
       
   350         _ = self._cw._
       
   351         self.columns = columns or self.columns
       
   352         ecls = self._cw.vreg['etypes'].etype_class(self.cw_rset.description[0][0])
       
   353         self.w(u'<table class="%s">' % self.table_css)
       
   354         self.table_header(ecls)
       
   355         self.w(u'<tbody>')
       
   356         for row in xrange(self.cw_rset.rowcount):
       
   357             self.cell_call(row=row, col=0)
       
   358         self.w(u'</tbody>')
       
   359         self.w(u'</table>')
       
   360 
       
   361     def cell_call(self, row, col):
       
   362         _ = self._cw._
       
   363         entity = self.cw_rset.get_entity(row, col)
       
   364         infos = {}
       
   365         for col in self.columns:
       
   366             meth = getattr(self, 'build_%s_cell' % col, None)
       
   367             # find the build method or try to find matching attribute
       
   368             if meth:
       
   369                 content = meth(entity)
       
   370             else:
       
   371                 content = entity.printable_value(col)
       
   372             infos[col] = content
       
   373         self.w(u"""<tr onmouseover="addElementClass(this, 'highlighted');"
       
   374             onmouseout="removeElementClass(this, 'highlighted')">""")
       
   375         line = u''.join(u'<td>%%(%s)s</td>' % col for col in self.columns)
       
   376         self.w(line % infos)
       
   377         self.w(u'</tr>\n')
       
   378 
       
   379     def table_header(self, ecls):
       
   380         """builds the table's header"""
       
   381         self.w(u'<thead><tr>')
       
   382         _ = self._cw._
       
   383         for column in self.columns:
       
   384             meth = getattr(self, 'header_for_%s' % column, None)
       
   385             if meth:
       
   386                 colname = meth(ecls)
       
   387             else:
       
   388                 colname = _(column)
       
   389             self.w(u'<th>%s</th>' % xml_escape(colname))
       
   390         self.w(u'</tr></thead>\n')
       
   391 
       
   392