Yes, of course you're right, I guess I wasn't clear enough. The real
problem is this: what if a user needs to change the value of select_1 to a
different one in order to update their data? Then, depending on what they
choose, select_2 should still be auto-populated.
I don't get why code which works for insert form fails when it comes to
update. Can it be connected to the fact that the update form shows specific
value of select_1? Not sure. But jquery change() should anyway spot the
select change action and trigger ajax().
On Saturday, June 22, 2013 7:39:05 PM UTC+2, Anthony wrote:
>
> $('#select_1').change(function()
>
> You're using a "change" event handler, so it will only fire when the value
> of select_1 is changed. When the update form is first loaded, the existing
> record's value for select_1 is shown, but there is no change event to
> trigger the retrieval of the select_2 values.
>
> Note, for an update form, it should be possible to populate select_2 on
> the server rather than via ajax, since you already have the value of
> select_1.
>
> Anthony
>
> On Saturday, June 22, 2013 1:29:02 PM UTC-4, 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.