Skip to content

Commit 9f67c69

Browse files
committed
Examples working and updated in the html
1 parent 93360cd commit 9f67c69

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

autocomplete.html

+49-36
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ <h3>A really simple example</h3>
4040
list: birds_list
4141
})
4242
</code></pre>
43-
<h3>More advanced remote JSON example</h3>
43+
<h3>Remote JSON example</h3>
4444
<p>Basic tweaking and getting the results from a remote web service</p>
4545
<ul>
4646
<li>Changing the default list elements type by passing the .text object to matcher and 'insertText' / 'templateText'.</li>
4747
<li>Overriding the default "match" function with a case-insensitive version</li>
4848
<li>Using a custom template for the results box (through the "templateText")</li>
49+
<li>Uses the <code>autocomplete.ext</code> ajax and templateText plugins.
4950
</ul>
5051
<form onsubmit="alert('Supposed to submit now...'); return false;" action="/">
5152
<p>
@@ -63,21 +64,26 @@ <h3>More advanced remote JSON example</h3>
6364
<p>The source code :</p>
6465
<pre class="prettyprint"><code>
6566
$("input.autocomplete.big-cats").autocomplete({
66-
ajax: "list",
67-
timeout: 500,
68-
threshold: 2,
69-
match: function(typed) { return this.text.match(new RegExp(typed, "i")); },
70-
insertText: function(obj) { return obj.text },
71-
templateText: "&lt;li&gt;Available cats: <%= text %>&lt;/li&gt;"
72-
})
73-
</code></pre>
67+
ajax: "list",
68+
match: function(element, matcher) {
69+
return element.text.match(matcher);
70+
},
71+
insertText: function(obj) {
72+
return obj.text;
73+
},
74+
templateText: "&lt;li&gt;Available cats: &lt;%= text %&gt;&lt;/li&gt;"
75+
});
76+
77+
</code></pre>
7478

7579
<h3>A more advanced example</h3>
7680
<p>
7781
Tweaking the inner working of the plugin (as above) but with more powerful options
7882
(notice the "match" and the "templateText" options).
79-
<br/>
80-
The possibilities are endless...
83+
<ul>
84+
<li>Modifies the once-per-input matcher to carry information about the text typed.</li>
85+
<li>The match function adds information to the matched element to be used when templating it.</li>
86+
</ul>
8187
</p>
8288
<form onsubmit="alert('Supposed to submit now...'); return false;" action="/">
8389
<p>
@@ -93,31 +99,38 @@ <h3>A more advanced example</h3>
9399
</p>
94100
</form>
95101
<p>The source code :</p>
96-
<pre class="prettyprint"><code>
97-
var weird_names_list = [{text: 'Curious George'}, {text: 'George of the Jungle'}, {text: 'Felix the Cat'}];
98-
$("input.autocomplete.weird-names").autocomplete({
99-
list: weird_names_list,
100-
timeout: 0,
101-
match: function(typed) {
102-
this.typed = typed;
103-
this.pre_match = this.text;
104-
this.match = this.post_match = '';
105-
if (!this.ajax &amp;&amp; !typed || typed.length == 0) { return true; }
106-
var match_at = this.text.search(new RegExp("\\b" + typed, "i"));
107-
if (match_at != -1) {
108-
this.pre_match = this.text.slice(0,match_at);
109-
this.match = this.text.slice(match_at,match_at + typed.length);
110-
this.post_match = this.text.slice(match_at + typed.length);
111-
return true;
112-
}
113-
return false;
114-
},
115-
insertText: function(obj) { return obj.text },
116-
templateText: "&lt;li&gt;<%= pre_match %>&lt;span class='matching' &gt;<%= match %>&lt;/span&gt;<%= post_match %>&lt;/li&gt;"
117-
});
118-
</code></pre>
102+
<pre class="prettyprint"><code>
103+
var weird_names_list = [{text: 'Curious George'}, {text: 'George of the Jungle'}, {text: 'Felix the Cat'}];
104+
$("input.autocomplete.weird-names").autocomplete({
105+
list: weird_names_list,
106+
timeout: 0,
107+
matcher: function(typed) {
108+
if (!typed || typed.length == 0) return undefined;
109+
var reg = new RegExp("\\b" + typed, "i");
110+
reg.typed = typed;
111+
return reg;
112+
},
113+
match: function(element, matcher) {
114+
if (!matcher) { return false; }
115+
var typed = matcher.typed;
116+
element.typed = typed;
117+
element.pre_match = element.text;
118+
element.match = element.post_match = '';
119+
var match_at = element.text.search(matcher);
120+
if (match_at != -1) {
121+
element.pre_match = element.text.slice(0,match_at);
122+
element.match = element.text.slice(match_at,match_at + typed.length);
123+
element.post_match = element.text.slice(match_at + typed.length);
124+
return true;
125+
}
126+
return false;
127+
},
128+
insertText: function(obj) { return obj.text; },
129+
templateText: "&lt;li&gt;&lt;%= pre_match %&gt;&lt;span class='matching' &gt;&lt;%= match %&gt;&lt;/span&gt;&lt;%= post_match %&gt;&lt;/li&gt;"
130+
});
131+
</code></pre>
119132
<h3>Width of the autocomplete box</h3>
120-
<p>Did you find too big bird names? Lets autoadjust the size of the autocomplete box.</p>
133+
<p>Did you find too big bird names? Let's the css adjust the size of the autocomplete box.</p>
121134
<form onsubmit="alert('Supposed to submit now...'); return false;" action="/">
122135
<label for="bird-name2">
123136
Local list
@@ -160,7 +173,7 @@ <h2>Info</h2>
160173
<li>http://www.gnu.org/licenses/gpl.html</li>
161174
</ul>
162175

163-
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
176+
<script type="text/javascript" src="jquery-1.4.2.js"></script>
164177
<script type="text/javascript" src="jquery.templating.js"></script>
165178
<script type="text/javascript" src="jquery.ui.autocomplete.ext.js"></script>
166179
<script type="text/javascript" src="jquery.ui.autocomplete.js"></script>

autocomplete.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jQuery(function($) {
2727
wrapper: '<ul class="jq-ui-autocomplete mybigbirdlist"></ul>',
2828
});
2929

30-
//
30+
// Simple ajax example
3131
$("input.autocomplete.big-cats").autocomplete({
3232
ajax: "list",
3333
match: function(element, matcher) {
@@ -39,6 +39,7 @@ jQuery(function($) {
3939
templateText: "<li>Available cats: <%= text %></li>"
4040
});
4141

42+
// Advanced matching example
4243
var weird_names_list = [{text: 'Curious George'}, {text: 'George of the Jungle'}, {text: 'Felix the Cat'}];
4344
$("input.autocomplete.weird-names").autocomplete({
4445
list: weird_names_list,
@@ -67,10 +68,11 @@ jQuery(function($) {
6768
insertText: function(obj) { return obj.text; },
6869
templateText: "<li><%= pre_match %><span class='matching' ><%= match %></span><%= post_match %></li>"
6970
});
70-
71+
// Console bindings
7172
$("input.autocomplete")
7273
.bind("activated.autocomplete", function(e, d) { console.log("activated.autocomplete: "+d); })
7374
.bind("cancelled.autocomplete", function(e) { console.log("Cancelled"); });
74-
75-
prettyPrint();
75+
76+
// Put colors in the <pre><Code> for the code samples
77+
prettyPrint();
7678
});

0 commit comments

Comments
 (0)