how to use JqKeypress?

15 views
Skip to first unread message

harryh

unread,
Oct 19, 2009, 8:13:37 PM10/19/09
to Lift
It seems like I ought to be able to use JqKeypress so that if a user
presses enter when a text input has focus an associated ajaxButton is
submitted. I can't get it to work though. Help?

Basically I have a bunch of little form like so

[ text input ] [go!]

that I want to submit (ajax style) when the user presses enter.

-harryh

Derek Chen-Becker

unread,
Oct 20, 2009, 11:27:49 AM10/20/09
to lif...@googlegroups.com
I think that the ajaxText already handles an "enter" key as field submission. Here's the code that defines an ajaxText field:

  1.   private def ajaxText_*(value: String, jsFunc: Box[Call], func: AFuncHolder, attrs: (String, String)*): Elem = {  
  2.     val raw = (funcName: String, value:String) => JsRaw("'" +funcName + "=' + encodeURIComponent(" + value + ".value)")  
  3.     val key = formFuncName  
  4.   
  5.     fmapFunc(func){  
  6.       funcName =>  
  7.       (attrs.foldLeft(<input type="text" value={value}/>)(_ % _)) %  
  8.       ("onkeypress" -> """liftUtils.lift_blurIfReturn(event)""") %  
  9.       ("onblur" -> (jsFunc match {  
  10.             case Full(f) => JsCrVar(key, JsRaw("this")) & deferCall(raw(funcName, key), f)  
  11.             case _ => makeAjaxCall(raw(funcName, "this"))  
  12.           })  
  13.       )  
  14.     }  
  15.   }

Note the "onkeypress" and "onblur" event handlers. If you need to do some extra handling on the client side, you can provide the jsFunc parameter which will be used to submit the ajax call.

Derek

harryh

unread,
Oct 20, 2009, 3:05:25 PM10/20/09
to Lift
Yes, but then how can I distinguish back on the server whether func is
being executed because of a blur (in which case I don't really want to
do anything) or because of a keypress of enter (in which case I want
to perform an action as if the go button had been pressed).

-harryh

On Oct 20, 11:27 am, Derek Chen-Becker <dchenbec...@gmail.com> wrote:
> I think that the ajaxText already handles an "enter" key as field
> submission. Here's the code that defines an ajaxText field:
>
>    1.   private def
>     ajaxText_*(value: String, jsFunc: Box[Call], func: AFuncHolder,
> attrs: (String, String)*): Elem = {
>    2.     val raw = (funcName: String, value:String) => JsRaw("'"
>     +funcName + "=' + encodeURIComponent(" + value + ".value)")
>    3.     val key = formFuncName
>    4.
>    5.     fmapFunc(func){
>    6.       funcName =>
>    7.       (attrs.foldLeft(<input type="text" value={value}/>)(_ % _)) %
>    8.       ("onkeypress" -> """liftUtils.lift_blurIfReturn(event)""") %
>    9.       ("onblur" -> (jsFunc match {
>    10.             case Full(f) => JsCrVar(key, JsRaw("this"
>    )) & deferCall(raw(funcName, key), f)
>    11.             case _ => makeAjaxCall(raw(funcName, "this"))
>    12.           })
>    13.       )
>    14.     }
>    15.   }

Derek Chen-Becker

unread,
Oct 20, 2009, 4:01:40 PM10/20/09
to lif...@googlegroups.com
With the current function you can't. I can see a need for having the ajax fired on key press and not on blur, so please open an issue and reference this conversation.

Derek
Reply all
Reply to author
Forward
0 new messages