You are definitely correct. I mis-read what you had, but what I put
up, you can still use as well if you need to pass args via the URL.
In any case, if I am understanding this correctly, it seems to be more
of a problem of getting the selected value of the option, correct?
There are probably several ways to do this (I actually think jQuery
may provide an easy way to get at the selected option). Off the top
of my head, this is probably what I would do:
- Create a hidden input variable in the form to hold the selected
value
- before the ajax call, populate the hidden input field with what
selected option's value
Something like this would need to be added to your code:
In the View:
- Add this onto your form: <input type="hidden" id="hdnSel"
name="hdnSel" value="">
- Add this to the onChange event: _onChange="$
('#hdnSel').val(this.value); ajax('test_chg',['hdnSel'],'target');",
What this does is on the onChange event, it will populate the hidden
input we added with the selected option's value. This is then passed
through the AJAX call. Hopefully this helps. Let me know :)
On Nov 9, 8:28 pm, oneroler <[email protected]> wrote:
> Alex,
>
> Thanks. I thought based on the book that the 2nd argument of ajax was
> passing the values to request.vars.variable. I pasted some of the
> code from the book below for reference (http://web2py.com/book/default/
> chapter/10?search=ajax#The-ajax-Function). Based on this I originally
> had the code below in test_chg. Am I mistaken about what the 2nd
> argument is? Also, regarding your suggestion, could you tell me how
> to identify the selected item so that I can pass as an args/vars in
> the URL as you recommended?
>
> def one():
> return dict()
>
> def echo():
> return request.vars.name
>
> {{extend 'layout.html'}}
> <form>
> <input id="name" onkeyup="ajax('echo', ['name'], 'target')" />
> </form>
> <div id="target"></div>
>
> def test_chg
> firm=request.vars.firmdrop
> strats = db(db.firmstrat.firm_id==firm).select()
> options=[]
> for strat in strats:
> options.append(strat.strat_id.name)
> return DIV(*options).xml()
>
> On Nov 8, 9:37 pm, Alex <[email protected]> wrote:
>
>
>
> > How are you passing the value in the request.vars?
>
> > I'm going to guess that on your AJAX call in the _onChange handler,
> > you want to specify the parameters. You can do something like:
>
> > - ajax( '{{=URL(c='controllerName',f='test_chg',
> > args=request.args(0))}}', ['firmdrop'],'target' )
>
> > You can pass whatever you want in args. If you have values in
> > request.vars, you should be able to access then like:
>
> > - request.vars.variableName
>
> > On Nov 7, 9:11 pm, oneroler <[email protected]> wrote:
>
> > > I'm trying to learn to use the ajax functionality and am having
> > > trouble getting it to work correctly. I am trying to use a dropdown
> > > of firms to show the related strategies. Currently this doesn't
> > > return anything to the div, but I do know that the onchange is
> > > working. It seems like the request.vars.values()[0] is not returning
> > > anything. Any help would be appreciated and also any pointers if
> > > there is a way to do this better differently. Below is my code.
>
> > > I have based this on this
> > > messagehttp://groups.google.com/group/web2py/browse_thread/thread/4a6149ddcb....
>
> > > <<default.py>>
> > > def test():
> > > firms = db(db.firm).select()
> > > return dict(firms=firms)
>
> > > def test_chg():
> > > firm=request.vars.values()[0]
> > > strats = db(db.firmstrat.firm_id==firm).select()
> > > options=[]
> > > for strat in strats:
> > > options.append(strat.strat_id.name)
> > > return DIV(*options).xml()
>
> > > <<test.html>>
> > > {{extend 'layout.html'}}
> > > {{=SELECT(_id='firmdrop',
> > > _onChange="ajax('test_chg',['firmdrop'],'target');",
> > > *[OPTION(firms[i].name,_value=str(firms[i].id)) for i in
> > > range(len(firms))]
> > > )}}
> > > <div id="target"></div>
>
> > > Thanks,
> > > Sam- Hide quoted text -
>
> - Show quoted text -