Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Autocomplete Add example for using source callback, along with a note ao... #49

Closed
wants to merge 1 commit into from

2 participants

@jzaefferer
Owner

...but the escapeRegex method. Fixes #41

@scottgonzalez scottgonzalez commented on the diff
entries/autocomplete.xml
@@ -168,5 +169,23 @@ $( "#autocomplete" ).autocomplete({
<input id="autocomplete">
]]></html>
</example>
+ <example>
+ <desc>Using a custom source callback to match only the beginning of terms</desc>
+ <code><![CDATA[
+var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
+$( "#autocomplete" ).autocomplete({
+ source: function(request, response) {
@scottgonzalez Owner

spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@scottgonzalez scottgonzalez commented on the diff
entries/autocomplete.xml
@@ -74,6 +74,7 @@
<li>A <code>request</code> object, with a single <code>term</code> property, which refers to the value currently in the text input. For example, if the user enters <code>"new yo"</code> in a city field, the Autocomplete term will equal <code>"new yo"</code>.</li>
<li>A <code>response</code> callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the <code>response</code> callback even if you encounter an error. This ensures that the widget always has the correct state.</li>
</ul>
+ When filtering data locally, you can make use of the built-in <code>$.ui.autocomplete.escapeRegex</code> function. It'll take a single string argument and escape all regex characters, making the result safe to pass to <code>new RegExp()</code>.
@scottgonzalez Owner

<p>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@scottgonzalez

Looks good.

@jzaefferer
Owner

Landed in 9eeea1c

@jzaefferer jzaefferer closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 13, 2012
  1. @jzaefferer

    Autocomplete Add example for using source callback, along with a note…

    jzaefferer authored
    … aobut the escapeRegex method. Fixes #41
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 0 deletions.
  1. +19 −0 entries/autocomplete.xml
View
19 entries/autocomplete.xml
@@ -74,6 +74,7 @@
<li>A <code>request</code> object, with a single <code>term</code> property, which refers to the value currently in the text input. For example, if the user enters <code>"new yo"</code> in a city field, the Autocomplete term will equal <code>"new yo"</code>.</li>
<li>A <code>response</code> callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the <code>response</code> callback even if you encounter an error. This ensures that the widget always has the correct state.</li>
</ul>
+ When filtering data locally, you can make use of the built-in <code>$.ui.autocomplete.escapeRegex</code> function. It'll take a single string argument and escape all regex characters, making the result safe to pass to <code>new RegExp()</code>.
@scottgonzalez Owner

<p>?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
</desc>
</type>
</option>
@@ -168,5 +169,23 @@ $( "#autocomplete" ).autocomplete({
<input id="autocomplete">
]]></html>
</example>
+ <example>
+ <desc>Using a custom source callback to match only the beginning of terms</desc>
+ <code><![CDATA[
+var tags = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ];
+$( "#autocomplete" ).autocomplete({
+ source: function(request, response) {
@scottgonzalez Owner

spaces

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
+ response( $.grep( tags, function( item ){
+ return matcher.test( item );
+ }) );
+ }
+});
+]]></code>
+ <html><![CDATA[
+<label for="autocomplete">Select a programming language: </label>
+<input id="autocomplete">
+]]></html>
+ </example>
<category slug="widgets"/>
</entry>
Something went wrong with that request. Please try again.