Note that there are some explanatory texts on larger screens.

plurals
  1. POjquery ui combobox results sorting
    primarykey
    data
    text
    <p>I have a <b>Jquery UI Combobox autocomplete widget</b>, and I'm trying to write a <b>matcher </b> function that will sort search results. I'm trying to get the preferred result:</p> <p>When you type for eg. <b>lo</b> the results should be sorted this way:</p> <ul> <li><b>Lo</b>ndon </li> <li>Bo<b>lo</b>gna </li> <li>Barce<b>lo</b>na </li> </ul> <p>When it's term that matches the first typed letters, it's on the first place, when it's term that contains letters it appears after the first rule.</p> <p>I've found something similar but I can't make it work. I can't find a way to establish a relation between my <b>matcher</b> variable and <b>term</b> variable in this solution.</p> <h3>Combobox original</h3> <pre class="lang-js prettyprint-override"><code>source: function( request, response ) { // match by first letters in search term var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex(request.term), "i" ); response( select.children( "option" ).map(function() { var text = $( this ).text(); if ( this.value &amp;&amp; ( !request.term || matcher.test(text) ) ) return { label: text.replace( new RegExp( "(?![^&amp;;]+;)(?!&lt;[^&lt;&gt;]*)(" + $.ui.autocomplete.escapeRegex(request.term) + ")(?![^&lt;&gt;]*&gt;)(?![^&amp;;]+;)", "gi" ), "&lt;strong&gt;$1&lt;/strong&gt;" ), value: text, option: this }; }) ); }, </code></pre> <h3>Solution for jquery ui autocomplete</h3> <pre class="lang-js prettyprint-override"><code>$(document).ready(function () { // my source is in html var source = ['Adam', 'Benjamin', 'Matt', 'Michael', 'Sam', 'Tim']; $("input").autocomplete({ source: function (request, response) { var term = $.ui.autocomplete.escapeRegex(request.term) , startsWithMatcher = new RegExp("^" + term, "i") , startsWith = $.grep(source, function(value) { return startsWithMatcher.test(value.label || value.value || value); }) , containsMatcher = new RegExp(term, "i") , contains = $.grep(source, function (value) { return $.inArray(value, startsWith) &lt; 0 &amp;&amp; containsMatcher.test(value.label || value.value || value); }); response(startsWith.concat(contains)); } }); }); </code></pre> <p><a href="https://stackoverflow.com/a/10948954/1650009">Link to similar answer I found</a> </p> <p><a href="http://jsfiddle.net/BRANIS/v6aWj/" rel="nofollow noreferrer"><b>JS FIDDLE</b></a></p> <p>Any possible feedback would be much appreciated.</p> <p>Thank you.</p>
    singulars
    1. This table or related slice is empty.
    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