[web2py] Re: Select with autocomplete
Massimo, Thanks, this basically does what I asked. Is the best way to get more advanced selection dropdowns to build a widget? What I'm really after is a dropdown more like the one linked here. http://developer.yahoo.com/yui/examples/autocomplete/ac_combobox.html Thanks again for you quick response. Best, Sam On Nov 14, 1:13 pm, mdipierro wrote: > as a test... leave min_lenght=0 and in gluon.sqlhtml.py after > > attr['_onkeyup'] = "" > > add > > attr['_onfocues'] = attr['_onkeyup'] > > (occurs in two places) > > Does it do what you ask? > > On Nov 14, 1:56 pm, oneroler wrote: > > > > > > > > > Is it possible to use the autocomplete widget on a select box where > > when someone clicks into the text field the dropdown with all the > > selections are available and then the user has the option to type and > > have the possible selections reduced? I have tried using the > > SQLFORM.widgets.autocomplete with min_length=0 and this kind of does > > what I want except that you have to start typing something to get the > > dropdown. I also tried the jquery autocomplete widget on > > web2pyslices, but couldn't get the result I wanted. I thought I had > > seen an answer to this in this group a while ago, but I haven't been > > able to find it so apologies if this has already been answered. > > Thanks in advance for any help. > > > Sam
[web2py] Select with autocomplete
Is it possible to use the autocomplete widget on a select box where when someone clicks into the text field the dropdown with all the selections are available and then the user has the option to type and have the possible selections reduced? I have tried using the SQLFORM.widgets.autocomplete with min_length=0 and this kind of does what I want except that you have to start typing something to get the dropdown. I also tried the jquery autocomplete widget on web2pyslices, but couldn't get the result I wanted. I thought I had seen an answer to this in this group a while ago, but I haven't been able to find it so apologies if this has already been answered. Thanks in advance for any help. Sam
[web2py] Re: Selecting from dropdown using Ajax
Alex, Thanks for all your help. Yes, you were correct that the issue was simply that the id value was not being passed in the ajax call. Here is my final code for reference if anyone is interested. <> def test(): firms = db(db.firm).select() return dict(firms=firms) def test_chg(): firm=request.vars.hdnSel strats = db(db.firmstrat.firm_id==firm).select() options=[] for strat in strats: options.append(strat.strat_id.name) return DIV(*options).xml() <> {{extend 'layout.html'}} {{=SELECT( _id='firmdrop', _onChange="$('#hdnSel').val(this.value);" "ajax('test_chg',['hdnSel'],'target');", *[OPTION(firms[i].name,_value=str(firms[i].id)) for i in range(len(firms))] )}} On Nov 9, 9:27 pm, Alex wrote: > 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: 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 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'}} > > > > > > > > > > > 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 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 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 > > > > > <> > > > > 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() > > > > > <> > > > > {{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))] > > > > )}} > > > > > > > > > Thanks, > > > > Sam- Hide quoted text - > > > - Show quoted text -
[web2py] Re: Selecting from dropdown using Ajax
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'}} 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 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 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 > > > <> > > 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() > > > <> > > {{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))] > > )}} > > > > > Thanks, > > Sam
[web2py] Selecting from dropdown using Ajax
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 message http://groups.google.com/group/web2py/browse_thread/thread/4a6149ddcbb768db#. <> 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() <> {{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))] )}} Thanks, Sam
[web2py] Crud validation
I have the following tables defined and want to make sure that when I use crud.create(db.firmstrat) I stop duplicate strategies for the same firm. What I have below works when firmstrat.firm_id is writable, but not when writable=False. It looks like the request.vars.firm_id = none when the field isn't writable for the crud form. Is there another way to access the information? Other thing I have tried that work are request.args(0) but I'm worried this may be an issue later if I'm trying to write to the table when args(0) isn't the firm_id. def show(): this_firm = db.firm(request.args(0)) or redirect(URL('index')) db.firmstrat.firm_id.default = this_firm.id form = crud.create(db.firmstrat) strats = db(db.firmstrat.firm_id==this_firm.id).select() return dict(strats=strats, firm=this_firm, form=form) db.define_table('firm', Field('name', unique=True, required=True)) db.define_table('lkfirmstrat', Field('name', unique=True)) db.define_table('firmstrat', Field('firm_id', db.firm, readable=False, writable=False), Field('strat_id',db.lkfirmstrat)) check = db(db.firmstrat.firm_id==request.vars.firm_id) db.firmstrat.strat_id.requires = IS_IN_DB(db, db.lkfirmstrat.id, '% (name)s', _and=IS_NOT_IN_DB(check, 'firmstrat.strat_id')) Thanks, Sam
[web2py] Re: Case insensitive is_not_in_db
Got it, that makes sense. Thanks a lot for the help. On Oct 16, 5:29 pm, mdipierro wrote: > the only way to do it efficiently would be to be this: > > db.define_table('firm', > Field('name'), > Field('lname',writable=False,readable=False)) > > def validate_name(form): > form.vars.lname,form.errors.name=IS_IN_DB(db,'firm.lname') > (form.vars.name.lower()) > > form=crud.create(db.first,onvalidation=validate_name) > > A little cumbersome but I am sure you get the idea. > > On Oct 16, 6:04 pm, oneroler wrote: > > > > > Thanks for the fast response. This works perfectly. You said using > > like is way less efficient, is there a way to use IS_NOT_IN_DB that > > would achieve the same goal? > > > On Oct 16, 3:45 pm, mdipierro wrote: > > > > Two things... > > > 1) > > > > db.firm.name.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, > > > 'firm.name')] > > > > and > > > > db.firm.name.requires = IS_NOT_IN_DB(db, 'firm.name') > > > > are equivalent. Is not in db, does not accept empty anyway. > > > Back to your question. > > > > You can make your own validator: > > > > class IS_NOT_IN_DB_ANYCASE: > > > def __init__(self,db, field, error_message='oops') > > > self.db=db > > > self.tablename,self.fieldname=field.split('.') > > > self.error_message=error_message > > > def __call__(self,value): > > > if self.db(self.db[self.tablename] > > > [self.fieldname].like(value)).count(): > > > return (value,self.error_message) > > > return (value,None) > > > > db.firm.name.requires = IS_NOT_IN_DB_ANYCASE(db, 'firm.name') > > > > but using like is way less efficient that not IS_NOT_IN_DB. > > > > On Oct 16, 5:18 pm, oneroler wrote: > > > > > I just started working with web2py and have what is probably a simple > > > > question on database validation. Below is what I have in db.py. > > > > Currently this will allow duplicate names that are in different cases > > > > (e.g. Firm 1 and firm 1). I want to make it so that it won't allow > > > > this but stores the name in the database the way the person enters it, > > > > so I don't want to convert everything to upper or lowercase and store > > > > it that way. Thanks in advance for any help. > > > > > db.define_table('firm', > > > > Field('name', unique=True)) > > > > > db.firm.name.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'firm.name')]
[web2py] Re: Case insensitive is_not_in_db
Thanks for the fast response. This works perfectly. You said using like is way less efficient, is there a way to use IS_NOT_IN_DB that would achieve the same goal? On Oct 16, 3:45 pm, mdipierro wrote: > Two things... > 1) > > db.firm.name.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, > 'firm.name')] > > and > > db.firm.name.requires = IS_NOT_IN_DB(db, 'firm.name') > > are equivalent. Is not in db, does not accept empty anyway. > Back to your question. > > You can make your own validator: > > class IS_NOT_IN_DB_ANYCASE: > def __init__(self,db, field, error_message='oops') > self.db=db > self.tablename,self.fieldname=field.split('.') > self.error_message=error_message > def __call__(self,value): > if self.db(self.db[self.tablename] > [self.fieldname].like(value)).count(): > return (value,self.error_message) > return (value,None) > > db.firm.name.requires = IS_NOT_IN_DB_ANYCASE(db, 'firm.name') > > but using like is way less efficient that not IS_NOT_IN_DB. > > On Oct 16, 5:18 pm, oneroler wrote: > > > > > I just started working with web2py and have what is probably a simple > > question on database validation. Below is what I have in db.py. > > Currently this will allow duplicate names that are in different cases > > (e.g. Firm 1 and firm 1). I want to make it so that it won't allow > > this but stores the name in the database the way the person enters it, > > so I don't want to convert everything to upper or lowercase and store > > it that way. Thanks in advance for any help. > > > db.define_table('firm', > > Field('name', unique=True)) > > > db.firm.name.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'firm.name')]
[web2py] Case insensitive is_not_in_db
I just started working with web2py and have what is probably a simple question on database validation. Below is what I have in db.py. Currently this will allow duplicate names that are in different cases (e.g. Firm 1 and firm 1). I want to make it so that it won't allow this but stores the name in the database the way the person enters it, so I don't want to convert everything to upper or lowercase and store it that way. Thanks in advance for any help. db.define_table('firm', Field('name', unique=True)) db.firm.name.requires = [IS_NOT_EMPTY(), IS_NOT_IN_DB(db, 'firm.name')]