Hi,
I'm trying to implement a drop-down based on the one at
http://www.web2pyslices.com/slice/show/1467/cascading-drop-down-lists-with-ajax
While the example above works great, I don't seem to be making a successful
ajax call in my adaptation.. Basically, the second dropdown does not reload
(it gets removed by jQuery .remove(), never to return) when the first
dropdown changes. if I remove this line from the above w2pslices example:
ajax('maker', ['category_name'], 'shadow_clone');
...then it fails just like my code does, making me think my ajax call does
not happen.
The db.district_data simply stores a title and an integer
('num_districts'). The districts always start at 1 and go up to
'num_districts' so if 'num_districts' is 3, I want to load the dropdown
with the three options 1 | 2 | 3
A user selects a title in the first dropdown and selects a district in the
second. I'm trying to use the ajax call to load the second dropdown based
on the contents of the first... so when a user selects a title from the
first dropdown, they should then see the other dropdown updated and loaded
with 1:num_districts (eg 1 | 2 | 3 ) for the given title.
Here's my code:
# model:
db.define_table('district_data',
Field('title', readable=False, writable=False),
Field('num_districts', 'integer', default=0, readable=False,
writable=False, requires=IS_INT_IN_RANGE(0, 1e100)),
auth.signature,
format = '%(title)s')
# controller:
@auth.requires_login()
def td():
titles_list = []
num_districts_list = []
q1 = auth.accessible_query('read', db.district_data)
records = db(q1).select()
for row in records:
titles_list.append(row.title)
num_districts_list.append( tuple(range(1, row.num_districts+1)) )
return(locals())
@auth.requires_login()
def maker():
with open('/home/JL/Desktop/results_dump.txt', 'w') as file:
file.write('abc' + '\n') # file untouched / does not execute.
q1 = auth.accessible_query('read', db.district_data)
q2 = db.district_data.title == request.vars.title_select.replace('_', '
') # not sure if this is necessary(?)
record = db(q1&q2).select().first()
result = "<select name='district_select'>"
district_choices = tuple(range(1, record.num_districts+1))
for dc in district_choices:
result += "<option value='" + str(dc) + "'>" + str(dc) +
"</option>"
result += "</select>"
return XML(result)
# view
{{extend 'layout.html'}}
<form enctype="multipart/form-data" action="{{URL()}}" method="post">
<select name='title_select'
onchange="jQuery(district_select).remove();
ajax('maker', ['title_select'], 'shadow_clone');">
{{for title in titles_list:}}
<option value="{{=title}}" {{=" selected='selected'" if
str(title)==request.vars.title_select else ""}}>
{{=title}}
</option>
{{pass}}
</select>
<span id='shadow_clone'></span>
<select name='district_select' >
{{for district in num_districts_list[0]:}}
<option value="{{=str(district)}}" {{=XML("
selected='selected'") if str(district)==str(request.vars.district_select)
else ""}}>
{{=str(district)}}
</option>
{{pass}}
</select>
<input type="submit" value='Submit'>
</form>
Thanks very much for looking at this,
-jl
--
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.