Hello All,
First things first: congratulations to Massimo and all those who
help.
Web2py is really a great framework.
Even for non-professional developers like me, it is both easy and
powerful.
A big big thank for providing such a nice framework.
I am trying to learn web2py (with an average Python level), so forgive
me for my silly questions (there will be others ;))
Could anybody help me with this SELECT / OPTGROUP problem ?
The following SELECT with OPTGROUP example illustrates my problems
(see below).
May be it is more a python difficulty than a web2py problem... (I am
not a professional).
1) How to make an item of the list "selected", for instance 'SubRegion
3' ? I tried with value= 'SubRegion 3' or selected='SubRegion 3' but
in this case, it doesn't work.
Idea: the visitor gives his region, then his region is automatically
pre-selected when he's in the search area.
He can change it too.
2) keepvalues=True does not work in my example (although it usually
works fine with INPUTs, SELECTs,...)
3) If someone has a more elegant way to program the building of the
select, I would appreciate ;)
4) Something else: I also made another form to generate a query (and
its result) with a sort order.
Is there a way to programmatically trigger the submit button when the
sort order is changed ?
(ie: I want the user to avoid having to click on submit after he
changed the sort order)
Thanks in advance for any help
Dominique
In models:
db.define_table('region',
Field('name', 'string', length=250))
db.define_table('subregion',
Field('name', 'string', length=250),
Field('region', db.region))
db.region.insert(name="Region A")
db.region.insert(name="Region B")
db.subregion.insert(name="SubRegion 1", region=1)
db.subregion.insert(name="SubRegion 2", region=1)
db.subregion.insert(name="SubRegion 3", region=2)
db.subregion.insert(name="SubRegion 4", region=2)
db.subregion.insert(name="SubRegion 5", region=2)
db.subregion.insert(name="SubRegion 6", region=2)
In Controller:
def search_form():
q = db(db.region.id==db.subregion.region)\
.select(orderby=db.region.name | db.subregion.name)
def get_it(rows):
"""
Creates a list of tuples to be used in the SELECT helper:
[('Region A',['bla bla bla SubRegion 1','bla bla bla
SubRegion 2', 'bla bla bla SubRegion 3']),
('Region A',['bla bla bla SubRegion 4','bla bla bla
SubRegion 5', 'bla bla bla SubRegion 6'])]
"""
alist=[]
for row in rows:
nr = row.region.name
if nr not in alist:
alist.append(nr)
the_list=[]
for elem in alist:
newlist=[]
for row in rows:
if row.region.name == elem:
newlist.append('bla bla bla '+row.subregion.name)
the_list.append([elem,OPTGROUP(*newlist)])
return the_list
t=None
form=FORM(
TR("",SELECT(*get_it(q), **dict(_name="region_to_search",
selected="bla bla bla SubRegion 3"))),
TR("",INPUT(_type="submit",_value="Search"))
)
if form.accepts(request.vars, session, keepvalues=True):
t=form.vars.region_to_search
response.flash="%s"%t
return dict(q=q, form=form)
All this returns something like (there are gluon objects inside...):
<select value="bla bla bla SubRegion 3">
<option value="('Region A', <gluon.html.OPTGROUP object
at 0x0715C090>)">Region A
<optgroup name="region_to_search">
<option value="bla bla bla SubRegion 1">bla bla bla SubRegion 1</
option>
<option value="bla bla bla SubRegion 2">bla bla bla SubRegion 2</
option>
</optgroup>
</option>
<option value="('Region B', <gluon.html.OPTGROUP object
at 0x0715CBF0>)">Region B<optgroup name="region_to_search">
<option value="bla bla bla SubRegion 3">bla bla bla SubRegion 3</
option>
<option value="bla bla bla SubRegion 4">bla bla bla SubRegion 4</
option>
<option value="bla bla bla SubRegion 5">bla bla bla SubRegion 5</
option>
<option value="bla bla bla SubRegion 6">bla bla bla SubRegion 6</
option>
</optgroup>
</option>
</select>