Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what you need to do.</p> <p>1) Make sure you are using Lift 2.5-SNAPSHOT (this is doable in earlier versions, but it's more difficult) </p> <p>2) In the snippet you use to render the page, use SHtml.ajaxCall (in particular, you probably want this version: <a href="https://github.com/lift/framework/blob/master/web/webkit/src/main/scala/net/liftweb/http/SHtml.scala#L170" rel="nofollow">https://github.com/lift/framework/blob/master/web/webkit/src/main/scala/net/liftweb/http/SHtml.scala#L170</a>) which will allow you to register a server side function that accepts your search term and return a JSON response containing the completions. You will also register some action to occur on the JSON response with the JsContext.</p> <p>3) The ajaxCall above will return a JsExp object which will result in the ajax request when it's invoked. Embed it within a javascript function on the page using your snippet.</p> <p>4) Wire them up with some client side JS.</p> <p><b>Update</b> - Some code to help you out. It can definitely be done more succinctly with Lift 2.5, but due to some inconsistencies in 2.4 I ended up rolling my own ajaxCall like function. S.fmapFunc registers the function on the server side and the function body makes a Lift ajax call from the client, then invokes the res function (which comes from jQuery autocomplete) on the JSON response.</p> <p>My jQuery plugin to "activate" the text input</p> <pre><code> (function($) { $.fn.initAssignment = function() { return this.autocomplete({ autoFocus: true, source: function(req, res) { search(req.term, res); }, select: function(event, ui) { assign(ui.item.value, function(data){ eval(data); }); event.preventDefault(); $(this).val(""); }, focus: function(event, ui) { event.preventDefault(); } }); } })(jQuery); </code></pre> <p>My Scala code that results in the javascript search function:</p> <pre><code> def autoCompleteJs = JsRaw(""" function search(term, res) { """ + (S.fmapFunc(S.contextFuncBuilder(SFuncHolder({ terms: String => val _candidates = if(terms != null && terms.trim() != "") assigneeCandidates(terms) else Nil JsonResponse(JArray(_candidates map { c => c.toJson })) }))) ({ name => "liftAjax.lift_ajaxHandler('" + name })) + "=' + encodeURIComponent(term), " + "function(data){ res(data); }" + ", null, 'json');" + """ } """) </code></pre> <p><b>Update 2</b> - To add the function above to your page, use a CssSelector transform similar to the one below. The >* means append to anything that already exists within the matched script element. I've got other functions I've defined on that page, and this adds the search function to them.</p> <pre><code> "script >*" #> autoCompleteJs </code></pre> <p>You can view source to verify that it exists on the page and can be called just like any other JS function.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload