Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's my solution which I resolved a few months ago and just now have time to post it. In the future I hope to revise this Javascript to support option groups :)</p> <p>Basically I shoved all my JS off into a separate file and called my custom function passing in the <code>DropDownList</code> and <code>TextBox</code> for each respective Combobox I wanted to create. The <code>DataSourceID</code> was supplied to the <code>DropDownList</code> using an <code>SqlDataSource</code> to populate it from my database. ASP.NET renders this as a &lt;select> element with an &lt;option> element for each <code>ListItem</code> in the <code>DropDownList</code>. The jQuery UI autocomplete plugin reads these &lt;option> elements and creates a hidden &lt;ul> with a &lt;li> for each corresponding &lt;option> in the &lt;select> element. The items are found by the line <code>response($(ddl).children("option").map(function () {...</code>. Note that replacing 'children' with 'find' will return options inside of an &lt;optgroup> tag as well.</p> <p>Following is my markup which calls the JS function, followed by the JS function itself:</p> <pre class="lang-html prettyprint-override"><code>&lt;!-- Head section --&gt; &lt;script type="text/javascript"&gt; function pageLoad() { cbsetup("#TextBoxID", "#DropDownBoxID"); }); &lt;/script&gt; &lt;!-- Body section - SAMPLE COMBOBOX --&gt; &lt;span&gt; &lt;asp:DropDownList ID="DropDownBoxID" runat="server" DataSourceID="SDS" DataTextField="Name" DataValueField="ID"&gt;&lt;/asp:DropDownList&gt; &lt;asp:TextBox ID="TextBoxID" runat="server" CssClass="combobox ui-autocomplete-input ui-widget ui-widget-content ui-corner-left"&gt;&lt;/asp:TextBox&gt; &lt;!-- I know the button code is particularly nasty, no thanks to jQuery UI. I just copy/pasted for the most part --&gt; &lt;button id="ButtonID" type="button" tabindex="-1" title="Show All Items" class="toggle ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-right ui-button-icon" role="button" aria-disabled="false" style="margin:0 0 0 -5px; height: 23px;"&gt; &lt;span class="ui-button-icon-primary ui-icon ui-icon-triangle-1-s"&gt;&lt;/span&gt; &lt;span class="ui-button-text" style="padding:0;"&gt;&amp;nbsp;&lt;/span&gt; &lt;/button&gt; &lt;/span&gt; </code></pre> <pre class="lang-js prettyprint-override"><code>function cbsetup(cb, ddl) { try { $(cb).unbind(); $(cb).autocomplete({ delay: 300, minLength: 0, autoFocus: true, source: function (request, response) { var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); response($(ddl).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" ), "$1" //"&lt;style=\"text-weight:bold;\"&gt;$1&lt;/style&gt;" ), value: text, option: this }; })); }, select: function (event, ui) { ui.item.option.selected = true; }, change: function (event, ui) { if (!ui.item) { var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val()) + "$", "i"), valid = false; select.children("option").each(function () { if ($(this).text().match(matcher)) { this.selected = valid = true; return false; } }); if (!valid) { // remove invalid value, as it didn't match anything $(this).val(""); select.val(""); input.data("autocomplete").term = ""; return false; } } } }); $(cb).siblings("button.toggle").click(function () { // close if already visible if ($(cb).autocomplete("widget").is(":visible")) { $(cb).autocomplete("close"); return; } // work around a bug (likely same cause as #5265) $(this).blur(); // pass empty string as value to search for, displaying all results $(cb).autocomplete("search", ""); $(cb).focus(); }); } /* TRY END */ catch (e) { jAlert(e.name + ', ' + e.message, 'JS Exception Caught'); } } </code></pre>
 

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