FocusOnLoad in LiftScreen

152 views
Skip to first unread message

Scaalp

unread,
Mar 1, 2012, 3:38:05 PM3/1/12
to lif...@googlegroups.com
Hello,

is there a way to put the focus on a particular field when a form is presented to the user using a LiftScreen ?
I didn't find an answer in this list when searching for "FocusOnLoad" or "LiftScreen".

Thanks,
Pascal

Sören Kress

unread,
Mar 2, 2012, 5:30:54 PM3/2/12
to lif...@googlegroups.com
I didn't try it myself, but I guess you could add an attribute to your form field, e.g. a specific class name (like 'focus') or you could set the tabindex to 1 on that field. Then you should be able to identify that field via JavaScript and set the focus.

Best regards
Soeren

David Pollak

unread,
Mar 2, 2012, 5:33:52 PM3/2/12
to lif...@googlegroups.com
You could do something like:

override def screenTop = Full(Script(OnLoad(...)))

That would add a script to the page to do whatever you wanted.

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net


Scaalp

unread,
Mar 3, 2012, 4:08:47 AM3/3/12
to lif...@googlegroups.com
Thank you for this map with many paths to explore (screenTop, Script, OnLoad, ...).  With it, I should find my way to my goal.

Pascal


Le vendredi 2 mars 2012 23:33:52 UTC+1, David Pollak a écrit :
You could do something like:

override def screenTop = Full(Script(OnLoad(...)))

That would add a script to the page to do whatever you wanted.

Scaalp

unread,
Mar 3, 2012, 9:35:36 AM3/3/12
to lif...@googlegroups.com
Hello,

following the path given by David, I reached my goal with the following code :
override def screenTop = recipient.uniqueFieldId.map(id => <span>{Script(OnLoad(Focus(id)))}</span>)

But this seems a bit awkard to me, as I had to add a span, to transform the Node returned by Script into the Elem needed by screenTop.  Maybe should screenTop be of type Box[Node], instead of Box[Elem] ?

But I found another way that pleases me better : overwrite the toForm method of my LiftScreen :
override def toForm = recipient.uniqueFieldId.map(id => Script(OnLoad(Focus(id)))).toOption ++: super.toForm

Pascal

Chris Hagan

unread,
Jul 1, 2013, 5:29:52 AM7/1/13
to lif...@googlegroups.com
Brilliant!  I just found this a year later, and it's an elegant solution.

c
Reply all
Reply to author
Forward
0 new messages