@@ -40,12 +40,13 @@ <h3>A really simple example</h3>
40
40
list: birds_list
41
41
})
42
42
</ code > </ pre >
43
- < h3 > More advanced remote JSON example</ h3 >
43
+ < h3 > Remote JSON example</ h3 >
44
44
< p > Basic tweaking and getting the results from a remote web service</ p >
45
45
< ul >
46
46
< li > Changing the default list elements type by passing the .text object to matcher and 'insertText' / 'templateText'.</ li >
47
47
< li > Overriding the default "match" function with a case-insensitive version</ li >
48
48
< 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.
49
50
</ ul >
50
51
< form onsubmit ="alert('Supposed to submit now...'); return false; " action ="/ ">
51
52
< p >
@@ -63,21 +64,26 @@ <h3>More advanced remote JSON example</h3>
63
64
< p > The source code :</ p >
64
65
< pre class ="prettyprint "> < code >
65
66
$("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: "<li>Available cats: < %= text %> </li>"
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: "<li>Available cats: <%= text %></li>"
75
+ });
76
+
77
+ </ code > </ pre >
74
78
75
79
< h3 > A more advanced example</ h3 >
76
80
< p >
77
81
Tweaking the inner working of the plugin (as above) but with more powerful options
78
82
(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 >
81
87
</ p >
82
88
< form onsubmit ="alert('Supposed to submit now...'); return false; " action ="/ ">
83
89
< p >
@@ -93,31 +99,38 @@ <h3>A more advanced example</h3>
93
99
</ p >
94
100
</ form >
95
101
< 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 && !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: "<li>< %= pre_match %> <span class='matching' >< %= match %> </span>< %= post_match %> </li>"
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: "<li><%= pre_match %><span class='matching' ><%= match %></span><%= post_match %></li>"
130
+ });
131
+ </ code > </ pre >
119
132
< 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 >
121
134
< form onsubmit ="alert('Supposed to submit now...'); return false; " action ="/ ">
122
135
< label for ="bird-name2 ">
123
136
Local list
@@ -160,7 +173,7 @@ <h2>Info</h2>
160
173
< li > http://www.gnu.org/licenses/gpl.html</ li >
161
174
</ ul >
162
175
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 >
164
177
< script type ="text/javascript " src ="jquery.templating.js "> </ script >
165
178
< script type ="text/javascript " src ="jquery.ui.autocomplete.ext.js "> </ script >
166
179
< script type ="text/javascript " src ="jquery.ui.autocomplete.js "> </ script >
0 commit comments