INPUT(..., _autofocus=True)
INPUT(..., _autofocus=True)
You'll get autofocus="autofocus" in the HTML, but that will work.
Anthony
On Wednesday, March 1, 2017 at 4:24:18 AM UTC-8, Anthony wrote:
INPUT(..., _autofocus=True)
You'll get autofocus="autofocus" in the HTML, but that will work.
AnthonyThanks! It does indeed work (Chrome Version 56.0.2924.87). I had remembered the underscore, but didn't think of the =True.
form.elements(input, _id=table_field, replace=lambda i: i[0:-2] + "autofocus" + i[-1])
For an SQLFORM(), things are a little trickier, because the **attributes applies to the whole FORM object, and can't be directed to the input field. So I tried using
form.elements(input, _id=table_field, replace=lambda i: i[0:-2] + "autofocus" + i[-1])but i is a gluon.html.INPUT object and not sliceable (the slices become empty strings); there is also a ticket for trying to concatenate a string to a list, just to keep me confused. Do I need to use javascript to set the focus?
form.element(...)['_autofocus'] = True
db.mytable.myfield.widget = lambda f, v: SQLFORM.widgets.string.widget(f, v, _autofocus=True)
/dps
For an SQLFORM(), things are a little trickier, because the **attributes applies to the whole FORM object, and can't be directed to the input field. So I tried using
form.elements(input, _id=table_field, replace=lambda i: i[0:-2] + "autofocus" + i[-1])but i is a gluon.html.INPUT object and not sliceable (the slices become empty strings); there is also a ticket for trying to concatenate a string to a list, just to keep me confused. Do I need to use javascript to set the focus?
You don't need to replace the input -- just update it:
form.element(...)['_autofocus'] = True
|
File C:\Users\Dave\web2py_win\web2py_win 2.14.6\web2py\gluon\html.py in elements at line 1104
(self=<gluon.sqlhtml.SQLFORM object>, *args=(<built-in function input>,), **kargs={'_id': 'no_table_image_url', 'first_only': True})
1103. | >>> b = a.elements('span.efg', find_text=re.compile('x|y|z'), replace='hello') |
But if it is a SQLFORM, a better approach is probably to change the widget associated with the DAL Field:
db.mytable.myfield.widget = lambda f, v: SQLFORM.widgets.string.widget(f, v, _autofocus=True)
If you always want autofocus set for that field, you can do the above within the DAL table definition.
Anthony
/dps
Just to keep things interesting, I tried this with a form from SQLFORM.factory() (used as a LOAD item). It keeps ending up in a ticket,
File "C:/Users/Dave/web2py_win/web2py_win 2.14.6/web2py/applications/badlist/controllers/default.py", line 46, in getpic
form.element(input, _id="no_table_image_url")['_autofocus'] = True
File "C:\Users\Dave\web2py_win\web2py_win 2.14.6\web2py\gluon\html.py", line 1200, in element
elements = self.elements(*args, **kargs)
File "C:\Users\Dave\web2py_win\web2py_win 2.14.6\web2py\gluon\html.py", line 1104, in elements
args = [a.strip() for a in args[0].split(',')]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
I tried setting the widget in the factoryfield def, and that doesn't seem to have an error, but doesn't autofocus. When the LOAD'ed form is submitted, I'm hoping to have the focus be on the factoryfield; when loading the page, the focus should be on the starting field of the main form (which is just SQLFORM()'d). [Edit:] Normal loading, and submitting of the main form, does indeed put focus on that starting field of the main form.
Just to keep things interesting, I tried this with a form from SQLFORM.factory() (used as a LOAD item). It keeps ending up in a ticket,
File "C:/Users/Dave/web2py_win/web2py_win 2.14.6/web2py/applications/badlist/controllers/default.py", line 46, in getpic
form.element(input, _id="no_table_image_url")['_autofocus'] = True
File "C:\Users\Dave\web2py_win\web2py_win 2.14.6\web2py\gluon\html.py", line 1200, in element
elements = self.elements(*args, **kargs)
File "C:\Users\Dave\web2py_win\web2py_win 2.14.6\web2py\gluon\html.py", line 1104, in elements
args = [a.strip() for a in args[0].split(',')]
AttributeError: 'builtin_function_or_method' object has no attribute 'split'
Should be form.element('input', ...), not form.element(input, ...).
I tried setting the widget in the factoryfield def, and that doesn't seem to have an error, but doesn't autofocus.
When the LOAD'ed form is submitted, I'm hoping to have the focus be on the factoryfield; when loading the page, the focus should be on the starting field of the main form (which is just SQLFORM()'d). [Edit:] Normal loading, and submitting of the main form, does indeed put focus on that starting field of the main form.
Need to see some code.
Anthony
{{left_sidebar_enabled,right_sidebar_enabled=False,('message' in globals())}}
{{extend 'layout.html'}}
[usual block header stuff]
[A couple of strings, with each having an {{=A(...)}} helper.]
{{=form}}
{{=getter}}
{{if len(response._vars)==1:}}
{{=response._vars.values()[0]}}
{{pass}}
default.py/index():
[housekeeping for PostDate]
form = SQLFORM(db.mylist)
if form.process().accepted:
response.flash = T("Thank you!")
elif form.errors:
response.flash = T("Please correct the errors in the form")
else:
response.flash = T("Please fill out the form")
return dict(form=form, getter=LOAD(url=URL(r=request,f='getitem.load'),ajax=True))
default.py/getitem():
form = SQLFORM.factory(
Field('item', requires=IS_NOT_EMPTY(), widget = lambda f, v: SQLFORM.widgets.string.widget(f, v, _autofocus=True)) ,
Field('tags'))
# form.element("input", _id="no_table_image_url")['_autofocus'] = True
if form.process().accepted:
response.flash = form.vars.image_url + ' accepted'
session.your_image = form.vars.image_url
details=figureoutdetails(form.vars.item)
newrow={'item': form.vars.item, 'details': details, 'tags' : form.vars.tags,
'PostDate':str(request.now.year)+"-"+str(request.now.month)+"-"+str(request.now.day)}
db.myitem.insert(**newrow)
elif form.errors:
response.flash = 'form has errors'
return dict(form=form)
I may be asking for moon ... expecting a "junior" autofocus to be "scoped" to the DIV it's form is loaded in. Browsers may instead consider 2 autofoci to be undefined behavior. In which case, turning to javascript may be my best choice. I've got my fingers crossed, though.
Oh, well.Slightly off-topic -- the insert() in the getitem function seems to skip the requires=[IS_NOT_IN_DB, ...] my model includes. Is that worth a new thread?