Thank you Val.

Ok now with in the select name='my_select' and in the Ajax call 
['my_select'] I can get the current value of my_select in the controller 
with request.vars['my_select']
I can return it:
def get_options_for_dropdown2():
    id = request.vars['my_select']
    return id

So in the page the target change, depending of the id that I've choose in 
my_select.

I can make a new set (many to many relation):
def get_options_for_dropdown2():
    id = request.vars['my_select']
    set_for_dropdown2 = 
db(db.my_first_datas_and_my_second_datas.ny_first_data == id)
    return id

But if I want to make a select on this set I have an error, but only in an 
Ajax call.
If I call the get_options_for_dropdown2 directly it works fine:
get_options_for_dropdown2?my_select=14

Final controller code is that one:
def get_options_for_dropdown2():
    id = request.vars['my_select']
    set_for_dropdown2 = 
db(db.my_first_datas_and_my_second_datas.my_first_data == id)
    html = ''
    for item in set_for_dropdown2.select():
        html += '<option value=' + str(item.my_first_data.id) + '>' 
+ item.my_first_data.name + '</option>' + '\r\n'
    return html

If I set internally in the function id = 14 it works.

Do you know why?

Thanks


Il giorno mercoledì 12 agosto 2015 19:35:47 UTC+2, Val K ha scritto:
>
> Yes! Of course! Because there is a small  bug in  web2py.js  in ajax() (or 
> in the manual)  :
> If  middle arg ( '[   ]' ) is string, it's treated as JQuery selector 
> without any processing!
> So ajax() call must be like: 
>  ajax('{{=URL('default','get_options_for_dropdown2')}}', '[name="name_1"], 
> [name="name_2"]', 'dropdown2')
>
> Here is example (just paste in any controller-file and call  '
> sender_receiver' ):
>
> def listener():
>     ans=UL(
>         LI('sel_1=%s'%request.vars.sel_1), 
>         LI('sel_2=%s'%request.vars.sel_2)
>         )
>     return ans #  - it can be a dict(ans=ans) if used  
> ajax('listener.load' ...    ) instead ajax('listener' ...    )
>
> def sender_receiver():
>     onchng="""ajax('listener' , '[name="sel_1"], [name="sel_2"]', 
> 'answer');   """
>     sel1=SELECT('opt1', 'opt2', 'opt3', _name='sel_1', _onchange=onchng)
>     sel2=SELECT('opt1', 'opt2', 'opt3', _name='sel_2', _onchange=onchng)
>     ans=DIV(_id='answer')
>     
>     return dict(sel1=sel1, sel2=sel2, ans=ans)
>
>
>
> After thought I've came to conclusion that In fact, there is no bug, but 
> just some hard to understand: JQuery treats  '[name]' - as all *inputs*  
> which have a *name*-attr  (i.e. any name) , so all inputs will be sended.
> In the book example: '<input .... name='name' .... id='name'  > ... 
> ajax(... '[name]' ...)    - who is who? Is what question is ;)
> so   '<input .... *name*='field' .... id='any_id'  > ... ajax(... '[*name*]' 
> ...)  - would be more readable
>
>
> On Wednesday, August 12, 2015 at 10:46:08 AM UTC+3, Gael Princivalle wrote:
>>
>> Thanks a lot Val that's certainly the solution, however in the controller 
>> the var is always empty.
>> This is the original select code:
>> <select name='ceo_scheme' onchange="$('#overrides').empty(); ajax(
>> '{{=URL('default','get_options_for_overrides')}}', ['ceo_scheme'], 
>> 'overrides');">
>>
>> If I set manually the var in get_options_for_overrides the ajax call 
>> works.
>> For debug, if I modify get_options_for_overrides for 
>> returning get_options_for_overrides.vars.ceo_scheme the target select 
>> 'override' is empty.
>>
>> My original controller:
>> def get_options_for_overrides():
>>     ceo_scheme_id = get_options_for_overrides.vars.ceo_scheme
>>     overrides = db(db.ceo_schemes_and_overrides.ceo_scheme == 
>> ceo_scheme_id).select(db.ceo_schemes_and_overrides.ALL)
>>     html = ''
>>     for override in overrides:
>>         html += '<option value=' + str(override.override.id) + '>' + 
>> override.override.name + '</option>' + '\r\n'
>>     return html
>>
>> My controller for debug:
>> def get_options_for_overrides():
>>     ceo_scheme_id = get_options_for_overrides.vars.ceo_scheme
>>     return ceo_scheme_id
>>
>> Do you know why?
>>
>> Regards.
>>
>> Il giorno martedì 11 agosto 2015 22:32:06 UTC+2, Val K ha scritto:
>>>
>>> Hello.
>>> Keep in mind that ajax() - is a simple web2py function  which is based 
>>> on Jquery (see web2py.js , its  code  is very short and clear)
>>> It can pass to controller  the values of the input elements only, see 
>>> http://api.jquery.com/serialize/. 
>>> So your select controls must have a  name   
>>> - <select name="any_name_1" ...
>>>
>>> To pass its  values just include its names in ['  '] -  ajax('{{=URL('
>>> default','get_options_for_dropdown2')}}', ['any_name_1,  any_name_2, ...
>>> '], 'dropdown2');">
>>> To get it in the controller: 
>>> get_options_for_dropdown2.vars.any_name_1
>>>
>>>
>>>
>>> On Tuesday, August 11, 2015 at 6:43:59 PM UTC+3, Gael Princivalle wrote:
>>>>
>>>> Hello.
>>>>
>>>> I'm still making a form like in this post:
>>>>
>>>> https://groups.google.com/forum/#!searchin/web2py/jquery$20db/web2py/tvfncYsLrQs/MRHG5MWOMlAJ
>>>>
>>>> <!-- DROPDOWN1-->
>>>> <select onchange="jQuery('#dropdown2').empty(); ajax('{{=URL('default
>>>> ','get_options_for_dropdown2')}}', [''], 'dropdown2');">
>>>>     <option...
>>>> </select>
>>>> <span id='dropdown2'>
>>>> <!-- DROPDOWN2-->
>>>> <select id='dropdown2'>
>>>>     <option...
>>>> </select>
>>>>
>>>> How is it possible to pass like a var the current select option value 
>>>> to the ajax call?
>>>>
>>>> With something like that?
>>>> data: {value: $('#select option:selected').val()}
>>>>
>>>> How it will be possible to retrieve this in the controller?
>>>>
>>>> Thanks.
>>>>
>>>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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/d/optout.

Reply via email to