github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

jquery / jquery-ui

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 533
    • 84
  • Source
  • Commits
  • Network (84)
  • Graphs
  • Tree: dbc9add

click here to add a description

click here to add a homepage

  • Switch Branches (7)
    • bind
    • devpreview
    • formcontrols
    • master
    • menu
    • panel
    • tooltip
  • Switch Tags (18)
    • 1.9m1
    • 1.8rc3
    • 1.8rc2
    • 1.8rc1
    • 1.8b1
    • 1.8a2
    • 1.8a1
    • 1.8.2
    • 1.8.1
    • 1.8
    • 1.7
    • 1.6rc6
    • 1.6rc5
    • 1.6rc3
    • 1.6rc2
    • 1.6
    • 1.5.2
    • 1.5.1
  • Comments
  • Contributors
Sending Request…

The official jQuery user interface library. — Read more

  Cancel

http://jqueryui.com/

  Cancel
  • HTTP
  • Git Read-Only

This URL has Read+Write access

Autocomplete: Refactored code for array filtering into $.ui.autocomplete.filter, 
used by remote-with-cache and modified multiple-demo (now with local data); 
added multiple-remote to also show multiple with remote data
jzaefferer (author)
Fri Apr 16 02:05:35 -0700 2010
commit  dbc9addfae0c9a2aee2d
tree    0bc04f8f3728b660b376
parent  cddf2a45da7195fadbe1
M demos/autocomplete/index.html 1 •
A demos/autocomplete/multiple-remote.html 75 •••••
M demos/autocomplete/multiple.html 22 ••••
M demos/autocomplete/remote-with-cache.html 7 ••••
M ui/jquery.ui.autocomplete.js 12 ••••
Txt demos/autocomplete/index.html
  • View file @ dbc9add
... ...
@@ -18,6 +18,7 @@
18 18
       <li><a href="xml.html">XML data parsed once</a></li>
19 19
       <li><a href="categories.html">Categories</a></li>
20 20
       <li><a href="multiple.html">Multiple values</a></li>
  21
+      <li><a href="multiple.html">Multiple, remote</a></li>
21 22
     </ul>
22 23
   </div>
23 24
 </body>
Txt demos/autocomplete/multiple-remote.html
  • View file @ dbc9add
... ...
@@ -0,0 +1,75 @@
  1
+<!DOCTYPE html>
  2
+<html lang="en">
  3
+<head>
  4
+  <meta charset="UTF-8" />
  5
+  <title>jQuery UI Autocomplete multiple demo</title>
  6
+  <link type="text/css" href="../../themes/base/jquery.ui.all.css" rel="stylesheet" />
  7
+  <script type="text/javascript" src="../../jquery-1.4.2.js"></script>
  8
+  <script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
  9
+  <script type="text/javascript" src="../../ui/jquery.ui.widget.js"></script>
  10
+  <script type="text/javascript" src="../../ui/jquery.ui.position.js"></script>
  11
+  <script type="text/javascript" src="../../ui/jquery.ui.autocomplete.js"></script>
  12
+  <link type="text/css" href="../demos.css" rel="stylesheet" />
  13
+  <script type="text/javascript">
  14
+  $(function() {
  15
+    function split(val) {
  16
+      return val.split(/,\s*/);
  17
+    }
  18
+    function extractLast(term) {
  19
+      return split(term).pop();
  20
+    }
  21
+    
  22
+    $("#birds").autocomplete({
  23
+      source: function(request, response) {
  24
+        $.getJSON("search.php", {
  25
+          term: extractLast(request.term)
  26
+        }, response);
  27
+      },
  28
+      search: function() {
  29
+        // custom minLength
  30
+        var term = extractLast(this.value);
  31
+        if (term.length < 2) {
  32
+          return false;
  33
+        }
  34
+      },
  35
+      focus: function() {
  36
+        // prevent value inserted on focus
  37
+        return false;
  38
+      },
  39
+      select: function(event, ui) {
  40
+        var terms = split( this.value );
  41
+        // remove the current input
  42
+        terms.pop();
  43
+        // add the selected item
  44
+        terms.push( ui.item.value );
  45
+        // add placeholder to get the comma-and-space at the end
  46
+        terms.push("");
  47
+        this.value = terms.join(", ");
  48
+        return false;
  49
+      }
  50
+    });
  51
+  });
  52
+  </script>
  53
+</head>
  54
+<body>
  55
+
  56
+<div class="demo">
  57
+
  58
+<div class="ui-widget">
  59
+  <label for="birds">Birds: </label>
  60
+  <input id="birds" size="50" />
  61
+</div>
  62
+
  63
+</div><!-- End demo -->
  64
+
  65
+<div class="demo-description">
  66
+<p>
  67
+Usage: Enter at least two characters to get bird name suggestions. Select a value to continue adding more names.
  68
+</p>
  69
+<p>
  70
+This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.
  71
+</p>
  72
+</div><!-- End demo-description -->
  73
+
  74
+</body>
  75
+</html>
Txt demos/autocomplete/multiple.html
  • View file @ dbc9add
... ...
@@ -12,6 +12,7 @@
12 12
   <link type="text/css" href="../demos.css" rel="stylesheet" />
13 13
   <script type="text/javascript">
14 14
   $(function() {
  15
+    var availableTags = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];
15 16
     function split(val) {
16 17
       return val.split(/,\s*/);
17 18
     }
... ...
@@ -19,18 +20,11 @@
19 20
       return split(term).pop();
20 21
     }
21 22
     
22  
-    $("#birds").autocomplete({
  23
+    $("#tags").autocomplete({
  24
+      minLength: 0,
23 25
       source: function(request, response) {
24  
-        $.getJSON("search.php", {
25  
-          term: extractLast(request.term)
26  
-        }, response);
27  
-      },
28  
-      search: function() {
29  
-        // custom minLength
30  
-        var term = extractLast(this.value);
31  
-        if (term.length < 2) {
32  
-          return false;
33  
-        }
  26
+        // delegate back to autocomplete, but extract the last term
  27
+        response($.ui.autocomplete.filter(availableTags, extractLast(request.term)));
34 28
       },
35 29
       focus: function() {
36 30
         // prevent value inserted on focus
... ...
@@ -56,15 +50,15 @@
56 50
 <div class="demo">
57 51
 
58 52
 <div class="ui-widget">
59  
-  <label for="birds">Birds: </label>
60  
-  <input id="birds" size="50" />
  53
+  <label for="tags">Tag programming languages: </label>
  54
+  <input id="tags" size="50" />
61 55
 </div>
62 56
 
63 57
 </div><!-- End demo -->
64 58
 
65 59
 <div class="demo-description">
66 60
 <p>
67  
-Usage: Enter at least two characters to get bird name suggestions. Select a value to continue adding more names.
  61
+Usage: Type something, eg. "j" to see suggestions for tagging with programming languages. Select a value, then continue typing to add more.
68 62
 </p>
69 63
 <p>
70 64
 This is an example showing how to use the source-option along with some events to enable autocompleting multiple values into a single field.
Txt demos/autocomplete/remote-with-cache.html
  • View file @ dbc9add
... ...
@@ -22,12 +22,11 @@
22 22
       source: function(request, response) {
23 23
         if (cache.term == request.term && cache.content) {
24 24
           response(cache.content);
  25
+          return;
25 26
         }
26 27
         if (new RegExp(cache.term).test(request.term) && cache.content && cache.content.length < 13) {
27  
-          var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
28  
-          response($.grep(cache.content, function(value) {
29  
-              return matcher.test(value.value)
30  
-          }));
  28
+          response($.ui.autocomplete.filter(cache.content, request.term));
  29
+          return;
31 30
         }
32 31
         $.ajax({
33 32
           url: "search.php",
Txt ui/jquery.ui.autocomplete.js
  • View file @ dbc9add
... ...
@@ -166,11 +166,7 @@ $.widget( "ui.autocomplete", {
166 166
     if ( $.isArray(this.options.source) ) {
167 167
       array = this.options.source;
168 168
       this.source = function( request, response ) {
169  
-        // escape regex characters
170  
-        var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
171  
-        response( $.grep( array, function(value) {
172  
-          return matcher.test( value.label || value.value || value );
173  
-        }) );
  169
+        response( $.ui.autocomplete.filter(array, request.term) );
174 170
       };
175 171
     } else if ( typeof this.options.source === "string" ) {
176 172
       url = this.options.source;
... ...
@@ -308,6 +304,12 @@ $.widget( "ui.autocomplete", {
308 304
 $.extend( $.ui.autocomplete, {
309 305
   escapeRegex: function( value ) {
310 306
     return value.replace( /([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1" );
  307
+  },
  308
+  filter: function(array, term) {
  309
+    var matcher = new RegExp( $.ui.autocomplete.escapeRegex(term), "i" );
  310
+    return $.grep( array, function(value) {
  311
+      return matcher.test( value.label || value.value || value );
  312
+    });
311 313
   }
312 314
 });
313 315
 

0 notes on commit dbc9add

Please log in to comment.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server