web/views/tableview.py
branchstable
changeset 2204 3472c5498478
parent 2115 c48ff1d0acc4
child 2206 009e2856afcb
equal deleted inserted replaced
2203:efdd3a9f9028 2204:3472c5498478
   103         """
   103         """
   104         rset = self.rset
   104         rset = self.rset
   105         req = self.req
   105         req = self.req
   106         req.add_js('jquery.tablesorter.js')
   106         req.add_js('jquery.tablesorter.js')
   107         req.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css'))
   107         req.add_css(('cubicweb.tablesorter.css', 'cubicweb.tableview.css'))
   108         rqlst = rset.syntax_tree()
       
   109         # get rql description first since the filter form may remove some
   108         # get rql description first since the filter form may remove some
   110         # necessary information
   109         # necessary information
   111         rqlstdescr = rqlst.get_description()[0] # XXX missing Union support
       
   112         mainindex = self.main_var_index()
   110         mainindex = self.main_var_index()
       
   111         computed_labels = rset.columns_labels(mainindex)
   113         hidden = True
   112         hidden = True
   114         if not subvid and 'subvid' in req.form:
   113         if not subvid and 'subvid' in req.form:
   115             subvid = req.form.pop('subvid')
   114             subvid = req.form.pop('subvid')
   116         divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(rset))
   115         divid = divid or req.form.get('divid') or 'rs%s' % make_uid(id(rset))
   117         actions = list(actions)
   116         actions = list(actions)
   152         # render actions menu
   151         # render actions menu
   153         if actions:
   152         if actions:
   154             self.render_actions(divid, actions)
   153             self.render_actions(divid, actions)
   155         # render table
   154         # render table
   156         table = TableWidget(self)
   155         table = TableWidget(self)
   157         for column in self.get_columns(rqlstdescr, displaycols, headers, subvid,
   156         for column in self.get_columns(computed_labels, displaycols, headers,
   158                                        cellvids, cellattrs, mainindex):
   157                                        subvid, cellvids, cellattrs, mainindex):
   159             table.append_column(column)
   158             table.append_column(column)
   160         table.render(self.w)
   159         table.render(self.w)
   161         self.w(u'</div>\n')
   160         self.w(u'</div>\n')
   162         if not fromformfilter:
   161         if not fromformfilter:
   163             self.w(u'</div>\n')
   162             self.w(u'</div>\n')
   186         for url, label, klass, ident in actions:
   185         for url, label, klass, ident in actions:
   187             menu.append(BoxLink(url, label, klass, ident=ident, escape=True))
   186             menu.append(BoxLink(url, label, klass, ident=ident, escape=True))
   188         box.render(w=self.w)
   187         box.render(w=self.w)
   189         self.w(u'<div class="clear"/>')
   188         self.w(u'<div class="clear"/>')
   190 
   189 
   191     def get_columns(self, rqlstdescr, displaycols, headers, subvid, cellvids,
   190     def get_columns(self, computed_labels, displaycols, headers, subvid,
   192                     cellattrs, mainindex):
   191                     cellvids, cellattrs, mainindex):
   193         columns = []
   192         columns = []
   194         for colindex, attr in enumerate(rqlstdescr):
   193         for colindex, label in enumerate(computed_labels):
   195             if colindex not in displaycols:
   194             if colindex not in displaycols:
   196                 continue
   195                 continue
   197             # compute column header
   196             # compute column header
   198             if headers is not None:
   197             if headers is not None:
   199                 label = headers[displaycols.index(colindex)]
   198                 label = headers[displaycols.index(colindex)]
   200             elif colindex == 0 or attr == 'Any': # find a better label
       
   201                 label = ','.join(display_name(self.req, et)
       
   202                                  for et in self.rset.column_types(colindex))
       
   203             else:
       
   204                 label = display_name(self.req, attr)
       
   205             if colindex == mainindex:
   199             if colindex == mainindex:
   206                 label += ' (%s)' % self.rset.rowcount
   200                 label += ' (%s)' % self.rset.rowcount
   207             column = TableColumn(label, colindex)
   201             column = TableColumn(label, colindex)
   208             coltype = self.rset.description[0][colindex]
   202             coltype = self.rset.description[0][colindex]
   209             # compute column cell view (if coltype is None, it's a left outer
   203             # compute column cell view (if coltype is None, it's a left outer
   212                 column.append_renderer(cellvids[colindex], colindex)
   206                 column.append_renderer(cellvids[colindex], colindex)
   213             elif coltype is not None and self.schema.eschema(coltype).is_final():
   207             elif coltype is not None and self.schema.eschema(coltype).is_final():
   214                 column.append_renderer(self.finalview, colindex)
   208                 column.append_renderer(self.finalview, colindex)
   215             else:
   209             else:
   216                 column.append_renderer(subvid or 'incontext', colindex)
   210                 column.append_renderer(subvid or 'incontext', colindex)
   217 
       
   218             if cellattrs and colindex in cellattrs:
   211             if cellattrs and colindex in cellattrs:
   219                 for name, value in cellattrs[colindex].iteritems():
   212                 for name, value in cellattrs[colindex].iteritems():
   220                     column.add_attr(name, value)
   213                     column.add_attr(name, value)
   221             # add column
   214             # add column
   222             columns.append(column)
   215             columns.append(column)