Frage

I'am trying to have auto complete and onBlur functionality attached to the same input field using Liftweb framework.

I have them working independently.

What I'am trying to do is have an auto complete input field and on selecting the value from the suggestion, some business logic is to be performed and another input field needs to be updated.

But only the auto complete feature is working.

This is the form

<form class="lift:CapitalOnBlur">
  Country : <input id="countryNameOnBlur" type="text" name="countryNameOnBlur"/><br />
  Capital: <input id="capitalNameOnBlur" type="text" name="capital"/>
</form>

This is the snippet

object CapitalOnBlur {

  val capitals: Map[String, String] = Map(
    "india" -> "New Delhi",
    "uganda" -> "Kampala",
    "japan" -> "Tokyo")

  def render = {

    def callback(countryName: String): JsCmd = {
      val capital = capitals.getOrElse(countryName.toLowerCase, "Not Found")
      SetValById("capitalNameOnBlur", capital)
    }

    val default = ""

    def suggest(value: String, limit: Int) = capitals.filter(_._1.startsWith(value.toLowerCase)).keys.toSeq

    def submit(value: String) = Unit

    "#countryNameOnBlur" #> AutoComplete(default, suggest, submit) &
    "#countryNameOnBlur [onBlur]" #> SHtml.onEvent(callback)
  }
}

This is what I actually want to do. I tried this and only onBlur event is triggered.

According to my needs, When I start typing the country name in the first input field, it should show me the suggestions and on selecting the suggestion i.e.; onBlur from that input field, the corresponding capital should be rendered in the next input field.

And also is there a way to trigger an action on selecting a suggestion using the inbuilt Auto complete feature of lift.

War es hilfreich?

Lösung

I am adding this as a separate answer since the edit is essentially a separate question. The AutoComplete widget from Lift does not modify an existing element on the page, but rather replaces it with the following NodeSeq, as per the source.

  <span>
    <head>
      <link rel="stylesheet" href={"/" + LiftRules.resourceServerPath +"/autocomplete/jquery.autocomplete.css"} type="text/css" />
      <script type="text/javascript" src={"/" + LiftRules.resourceServerPath +"/autocomplete/jquery.autocomplete.js"} />
      {Script(onLoad)}
    </head>
    <input type="text" id={id} value={default.openOr("")} />
    <input type="hidden" name={hidden} id={hidden} value={defaultNonce.openOr("")} />
  </span>

Since that has now replaced the original HTML, the second line where you add an onBlur handler is not applied to anything useful. However, the AutoComplete constructor does take an optional parameter for attributes and you can probably use that to add an onBlur attribute to the input tag.

You can try something like this:

"#countryNameOnBlur" #> AutoComplete(default, suggest, submit, 
  ("onBlur", SHtml.onEvent(callback).cmd.toJsCmd))

The above should pass in a tuple which specifies the attribute name, and the string representation of the Javascript you want executed. This should accomplish what you are looking for as long as the AutoComplete library doesn't also rely on the onBlur event. That case is doable too, but a bit more work.

One other thing to note is that onBlur is fired when the input loses focus, ie: the user moves the cursor to another field. If you want it to fire any time the text changes, regardless of cursor position, you may prefer the onChange event.

Andere Tipps

If you are looking to bind to different events on the same element, so that you end up with something like: <input onblur="getCapitalName" onchange="autoComplete">, you can try using SHtml.onEvent. Something like this in your snippet should do the trick:

object CapitalOnBlur {
  def render = 
    "* [onblur]" #> SHtml.onEvent(e => CapitalOnBlur.getCapitalName(e)) &
    "* [onchange]" #> SHtml.onEvent(e => CapitalOnBlur.autoComplete(e)) &

  ...
}

And then call the snippet from your input, like this:

<form>
  Country : <input id="countryNameOnBlur" data-lift="CapitalOnBlur" type="text" name="countryNameOnBlur"/><br />
</form>

I am not sure what any of the arguments your code takes, so the above is mostly illustrative - but will hopefully get you on your way.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top