OK, just for the record: The reason my code below was not working for
update was wrong first argument of ajax() funcion. In my code the first
argument - url - wad relative, while it *should* be absolute. So all I
needed to do is change this:
<script>
$('#select_1').change(function() {
ajax('populate', ['department'], 'select_2');
});
</script>
to this:
{{=SCRIPT("$('#select_1').change(function() { ajax('%(url)s',
['department'], 'select_2'); });" % dict(url=URL('default', 'populate')))}}
This way, no matter where you include the script, it will always trigger
the right function from the right controller (in my case: *populate*).
On Saturday, June 22, 2013 7:29:02 PM UTC+2, lesssugar wrote:
>
> I have 2 select boxes. I managed to auto-populate second select box based
> on value from the first one. All of the code below works just fine when I
> create new record - second select is being populated. However, when I try
> to update the same record, select_2 does not get populated, as if the *
> department* variable never reached the *populate* function. As a result,
> there's a view rendered within select_2 instead of <option></option>
> elements. This occurs only in the update form.
>
> The script passing *department *variable from select_1:
>
> <script>
> $('#select_1').change(function() {
> ajax('populate', ['department'], 'select_2');
> });
> </script>
>
> *populate *function which uses "department" var to get respective data:
>
> def populate():
>
> ranks = db(db.rank.department_id == request.vars.department).select(
> db.rank.id, db.rank.name, orderby=db.rank.name)
>
> result = ""
>
> for r in ranks:
> result += "<option value='" + str(r.id) + "'>" + r.name +
> "</option>"
>
> return XML(result)
>
> view:
>
> <div class="w2p_fw">
> {{if request.vars.department:}}
> <select class="generic-widget" id="seafarer_experience_rank"
> name="rank">
> {{=XML(result)}}
> </select>
> {{else:}}
> {{=form.custom.widget.rank}}
> {{pass}}
> </div>
>
>
>
--
---
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.