I wrote a page with quite a few dropdown lists .. since it gets tedious to just copy and paste I wrote myself a DropDownList class which takes a name and default:
class DropDownListWithDefault(ElementWithData):
"""
Dropdown with data
data = { u'name': u'myddname',
u'namevaluelist': [[u'1', u'element 1], ..],
u'defaultVal': u'1'}
the namevaluelist will be taken as-is (no re-sorting,.. so It needs to be
a list of name-val lists which should be unicode strings ..
"""
docFactory = loaders.stan(T.span(render = u'items'))
@renderer
def items(self, req, tag):
ret = []
myname = u''
if self.data and self.data[u'name']:
myname = u'name'
# now for the data ...
dfltval = None
if self.data.has_key(u'defaultVal'):
dfltval = self.data[u'defaultVal']
if not self.data.get(u'namevaluelist'):
raise ValueError(u'No Name - Value list given or list empty')
for (val, nam) in self.data[u'namevaluelist']:
if val == dfltval:
ret.append(T.option(value = val, selected = u'selected')[nam])
else:
ret.append(T.option(value = val)[nam])
return T.select(name = myname, id_ = myname)[ret]
I couldn't find a way to assign name, id, .. afterwards to I just use a span with stan loader for renderer and then return my select. suggetsions welcome ;)
Comments
There are currently no comments
New Comment