Rishi
Rishi

Reputation: 1349

how to get autocomplete text box value using lift web

I am using Lift web framework.

I am implementing an auto-complete text box. When I enter some value in the box a drop-down list opens. If I select a value from that list, only then I am able to access value of text box. If I write a value by myself then I get an empty value.

My code :

var friend_name=""
"#bdayReminder" #> AutoComplete("",
        getAllName _,
        value => takeAction(value),
        List("minChars" -> "3"))


  private def takeAction(str: String) {
    friend_name = str

  }

Please suggest a solution

Upvotes: 1

Views: 393

Answers (1)

Brian Hsu
Brian Hsu

Reputation: 8821

Disclaimer: I'm the author of following library.

I think lift-combobox could achieve what you want, since it has a feature that let user created the value on-the fly. It use select2 jQuery plugin, so you will have a nice look and feel for the drop-down menu.

For example if you need to get the user-created value, it will simply as the following, note that we usually using Option[T] to denote that the value may not be presented, for example, the user may not selected any item in drop-menu at all:

var friend_name: Option[String] = None
val friendsMenu = new ComboBox(
  default = None,
  allowCreate = true
) {

  // This is where you build your combox suggestion
  override def onSearching(term: String): List[ComboItem] = {
    val names = List(
      ComboItem("f1", "Brian"), ComboItem("f2", "Alice"),
      ComboItem("f3", "Luke"), ComboItem("f4", "Smith"),
      ComboItem("f5", "Brandon")
    )

    names.filter(_.text.contains(term))
  }

  override def onItemSelected(selected: Option[ComboItem]): JsCmd = {
    friend_name = selected
    // The returned JsCmd will be executed on client side.
    Alert("You selected:" + selected)
  }

  // What you want to do if user added an item that
  // does not exist when allowCreate = true.
  override def onItemAdded(text: String): JsCmd = {
    friend_name = Some(text)
  }
}

"#bdayReminder" #> friendsMenu.combobox

Upvotes: 2

Related Questions