Note that there are some explanatory texts on larger screens.

plurals
  1. POLift - Autocomplete with Ajax Submission
    primarykey
    data
    text
    <p>I would like to use an autocomplete with ajax. So my goal is to have: </p> <ul> <li><p>When the user types something in the text field, some suggestions provided by the server appear (I have to find suggestions in a database)</p></li> <li><p>When the user presses "enter", clicks somewhere else than in the autocomplete box, or when he/she selects a suggestion, the string in the textfield is sent to the server. </p></li> </ul> <p>I first tried to use the autocomplete widget provided by lift but I faced three problems: </p> <ul> <li>it is meant to be an extended select, that is to say you can originally only submit suggested values. </li> <li>it is not meant to be used with ajax. </li> <li>it gets buggy when combined with <code>WiringUI</code>. </li> </ul> <p>So, my question is: How can I combine <a href="http://docs.jquery.com/Plugins/Autocomplete" rel="noreferrer">jquery autocomplete</a> and interact with the server in lift. I think I should use some callbacks but I don't master them. </p> <p>Thanks in advance. </p> <p><strong>UPDATE</strong> Here is a first implementation I tried but the callback doesn't work: </p> <pre><code>private def update_source(current: String, limit: Int) = { val results = if (current.length == 0) Nil else /* generate list of results */ new JsCmd{def toJsCmd = if(results.nonEmpty) results.mkString("[\"", "\", \"", "\"]") else "[]" } } def render = { val id = "my-autocomplete" val cb = SHtml.ajaxCall(JsRaw("request"), update_source(_, 4)) val script = Script(new JsCmd{ def toJsCmd = "$(function() {"+ "$(\"#"+id+"\").autocomplete({ "+ "autocomplete: on, "+ "source: function(request, response) {"+ "response("+cb._2.toJsCmd + ");" + "}"+ "})});" }) &lt;head&gt;&lt;script charset="utf-8"&gt; {script} &lt;/script&gt;&lt;/head&gt; ++ &lt;span id={id}&gt; {SHtml.ajaxText(init, s=&gt;{ /*set cell to value s*/; Noop}) } &lt;/span&gt; } </code></pre> <p>So my idea was: </p> <ul> <li>to get the selected result via an <code>SHtml.ajaxText</code> field which would be wraped into an autocomplete field</li> <li>to update the autocomplete suggestions using a javascript function</li> </ul>
    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. 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