form_factory focus

47 views
Skip to first unread message

annet

unread,
Jun 25, 2009, 11:55:13 AM6/25/09
to web2py Web Framework
In a function I have the following line of code:

form=form_factory(SQLField('plaats',requires=IS_NOT_EMPTY
(),widget=lambda self, value:INPUT
(_type='text',_id='clubbyplace',_class='ac_input',_name='plaats',requires=self.requires)))

When the view is displayed I would like the input field to have the
focus. How do I set the focus in case of from_factory?


Kind regards,

Annet.

mdipierro

unread,
Jun 25, 2009, 2:19:35 PM6/25/09
to web2py Web Framework

DenesL

unread,
Jun 25, 2009, 3:58:16 PM6/25/09
to web2py Web Framework
In the controller:

focus=SCRIPT("$(document).ready(function() { $('#clubbyplace').focus
() } )")
...
return dict(..., focus=focus)


and in the view somewhere:
{{=focus}}

Message has been deleted

annet

unread,
Jun 26, 2009, 6:51:37 AM6/26/09
to web2py Web Framework
Massimo,

When I add this line of code:

$("input:text:first:visble").focus();

to the core_ajax_init() function in web2py_ajax.html, the calendar
breaks when the first field in a form is of type date. Is there a way
to solve this.


Denes,

Using your solution, I don't have the problem described above, because
I set the focus per field. However when I set the focus on a field of
type date I run into the same problem.


Kind regards,

Annet.

DenesL

unread,
Jun 26, 2009, 8:48:50 AM6/26/09
to web2py Web Framework
The calendar field is probably using the onfocus event so my guess is
that you can not set the focus on those kind of fields.

JohnMc

unread,
Jun 26, 2009, 1:27:34 PM6/26/09
to web2py Web Framework
Annet,

DenesL is correct. web2py_ajax.html has the onfocus set to a callback
to invoke the calendar. Take DenesL code as he provided it, and in
the .focus() include a call back function for the Calendar code just
like it is in web2py_ajax.html. Should be a simple cut and paste.

JohnMc

annet

unread,
Jun 26, 2009, 1:55:26 PM6/26/09
to web2py Web Framework
I liked the $("input:text:first:visble").focus(); solution because one
line of code sets the focus on the first input field in all my forms,
in my cms application alone I have 32 forms. Only two of them have a
field of type date as the first field visible. In case of Denes's code
I would have to set the focus on each first field in every form
separately. I thought input:text meant input fields of type text only,
and that it would exclude input fields of type date.

I had hoped there would be a way to use Denes' code to take care of
these two exceptions.


Kind regards,

Annet.

Hans Donner

unread,
Jun 26, 2009, 2:33:44 PM6/26/09
to web...@googlegroups.com
annet, perhpas you can first retrieve the current onfocus value and
pass that back on your onfocus. This could be done for all forms.

mdipierro

unread,
Jun 26, 2009, 3:33:29 PM6/26/09
to web2py Web Framework
did you try?

jQuery(document).ready(function(){ jQuery
("input:text:first:visble").focus(); });

On Jun 26, 1:33 pm, Hans Donner <hans.don...@pobox.com> wrote:
> annet, perhpas you can first retrieve the current onfocus value and
> pass that back on your onfocus. This could be done for all forms.
>

DenesL

unread,
Jun 26, 2009, 3:59:16 PM6/26/09
to web2py Web Framework
On Jun 26, 6:51 am, annet <annet.verm...@gmail.com> wrote:
> Massimo,
>
> When I add this line of code:
>
> $("input:text:first:visible").focus();
>
> to the core_ajax_init() function in web2py_ajax.html, the calendar
> breaks when the first field in a form is of type date. Is there a way
> to solve this.

What do you mean by the calendar breaks?
I just had a little time to test and Firefox does not have a problem
focusing on a date field.

In strict HTMLese there is no type date, it is type text class date.
So you could change the selector to skip over class date fields (if
that is what you want):

$("input:text:not(.date):first:visible").focus();

Something nice to have:
http://acodingfool.typepad.com/blog/pdf/jquery_1.3_cheatsheet_v1.pdf

annet

unread,
Jun 27, 2009, 3:05:57 AM6/27/09
to web2py Web Framework
Hi Denes,

> What do you mean by the calendar breaks?
> I just had a little time to test and Firefox does not have a problem
> focusing on a date field.

In both Safari and Firefox, when I request a page with a field of type
date as the first field visible it has the focus, however, when I
click it the calendar doesn't show up, that is what I meant by the
calendar breaks.

> In strict HTMLese there is no type date, it is type text class date.
> So you could change the selector to skip over class date fields (if
> that is what you want):
>
> $("input:text:not(.date):first:visible").focus();

Thanks for your explanation, now I know why it doesn't work, and more
important, your solution solved the problem.
Yes indeed.


Massimo:

> did you try?
> jQuery(document).ready(function(){ jQuery
> ("input:text:first:visble").focus(); });

No I put:

jQuery("input:text:first:visble").focus();

in the function web2py_ajax_init() in the file web2py_ajax.html. Which
is called in:

jQuery(document).ready(web2py_ajax_init);

I thought that wouldn't make any difference.


Kind regards,

Annet.
Reply all
Reply to author
Forward
0 new messages