source: main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/widgets/multilistwidget.py @ 5302

Last change on this file since 5302 was 5302, checked in by uli, 14 years ago

Move widgets from jambtables to widgets module.

File size: 2.8 KB
Line 
1##
2## multilistwidget.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Jul 20 02:53:10 2010 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2010 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""A widget that renders multiple contained items horizontally.
23"""
24from zope.browserpage import ViewPageTemplateFile
25from zope.formlib.sequencewidget import (
26    ListSequenceWidget, SequenceDisplayWidget)
27from zope.formlib.widget import renderElement
28
29
30class MultiListWidget(ListSequenceWidget):
31    template = ViewPageTemplateFile('multilistwidget.pt')
32
33class MultiListDisplayWidget(SequenceDisplayWidget):
34    cssClass = "multiSequenceWidget"
35
36    def __call__(self):
37        # get the data to display:
38        if self._renderedValueSet():
39            data = self._data
40        else:
41            data = self.context.get(self.context.context)
42
43        # deal with special cases:
44        if data == self.context.missing_value:
45            return translate(self._missingValueMessage, self.request)
46        data = list(data)
47        if not data:
48            return translate(self._emptySequenceMessage, self.request)
49
50        parts = ['<table>']
51        for i, item in enumerate(data):
52            widget = self._getWidget(i)
53            if i == 0:
54                parts.append('<thead><tr>')
55                for x in widget.subwidgets():
56                    parts.append('<th>%s</th>' % x.label)
57                parts.append('</tr></thead><tbody>')
58            widget.setRenderedValue(item)
59            s = widget()
60            #print s
61            if self.itemTag:
62                #s = "<%s>%s</%s>" % (self.itemTag, s, self.itemTag)
63                evenodd = i%2 and 'odd' or 'even'
64                s = '<tr class="%s">%s</tr>' % (evenodd, s)
65            parts.append(s)
66        parts.append('</tbody></table>')
67        contents = "\n".join(parts)
68        if self.tag:
69            contents = "\n%s\n" % contents
70            contents = renderElement(self.tag,
71                                     cssClass=self.cssClass,
72                                     extra=self.extra,
73                                     contents=contents)
74        return contents
Note: See TracBrowser for help on using the repository browser.