An example of autocomplete with a server round trip to
calculate the autocomplete list
The Lift Scala code to render the controls:
classAjax {
defsample(xhtml: NodeSeq): NodeSeq = {
// local state for the counter
varcnt = 0
// get the id of some elements to update
valspanName: String = S.attr("id_name") openOr "cnt_id"valmsgName: String = S.attr("id_msgs") openOr "messages"// build up an ajax <a> tag to increment the counter
defdoClicker(text: NodeSeq) =
a(() => {cnt = cnt + 1; SetHtml(spanName, Text( cnt.toString))}, text)
// create an ajax select box
defdoSelect(msg: NodeSeq) =
ajaxSelect((1 to 50).toList.map(i => (i.toString, i.toString)),
Full(1.toString),
v => DisplayMessage(msgName,
bind("sel", msg, "number" -> Text(v)),
5 seconds, 1 second))
// build up an ajax text box
defdoText(msg: NodeSeq) =
ajaxText("", v => DisplayMessage(msgName,
bind("text", msg, "value" -> Text(v)),
4 seconds, 1 second))
// bind the view to the functionality
bind("ajax", xhtml,
"clicker" -> doClicker _,
"select" -> doSelect _,
"text" -> doText _,
"auto" -> JqSHtml.autocomplete("", buildQuery _, _ => ()))
}
privatedefbuildQuery(current: String, limit: Int): Seq[String] = {
Log.info("Checking on server side with "+current+" limit "+limit)
(1 to limit).map(n => current+""+n)
}
deftime = Text(timeNow.toString)
}
Lift is Copyright 2007-2010 WorldWide Conferencing, LLC. Distributed under an Apache 2.0 License.
Lift version 2.0-M3 built on Wed Mar 03 15:15:10 PST 2010.
Stats: Total Memory: 530,907,136
Free Memory: 476,869,176
Open Sessions: 356
Updated At: Thu Mar 11 13:04:25 PST 2010