-
Notifications
You must be signed in to change notification settings - Fork 38
/
JQueryUIAutocomplete.txt
256 lines (221 loc) · 9.53 KB
/
JQueryUIAutocomplete.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
%META:TOPICINFO{author="ProjectContributor" date="1456220586" format="1.1" version="1"}%
%META:TOPICPARENT{name="JQueryPlugin"}%
---+!! %TOPIC%
%JQPLUGINS{"ui::autocomplete"
format="
Homepage: $homepage <br />
Author(s): $author <br />
Version: $version
"
}%
%STARTSECTION{"summary"}%
Autocomplete, when added to an input field, enables users to quickly find and
select from a pre-populated list of values as they type, leveraging searching
and filtering.
By giving an Autocomplete field focus or entering something into it, the plugin
starts searching for entries that match and displays a list of values to choose
from. By entering more characters, the user can filter down the list to better
matches.
%ENDSECTION{"summary"}%
You can pull data in from a local and/or a remote source: Local is good for
small data sets (like an address book with 50 entries), remote is necessary for
big data sets, like a database with hundreds or millions of entries to select
from.
Autocomplete can be customized to work with various data sources, by just
specifying the source option. A data source can be:
* an Array with local data
* a String, specifying a URL
* a Callback
The local data can be a simple Array of Strings, or it contains Objects for
each item in the array, with either a label or value property or both. The
label property is displayed in the suggestion menu. The value will be inserted
into the input element after the user selected something from the menu. If just
one property is specified, it will be used for both, eg. if you provide only
value-properties, the value will also be used as the label.
When a String is used, the Autocomplete plugin expects that string to point to
a URL resource that will return JSON data. It can be on the same host or on a
different one (must provide JSONP). The request parameter "term" gets added to
that URL. The data itself can be in the same format as the local data described
above.
The third variation, the callback, provides the most flexibility, and can be
used to connect any data source to Autocomplete. The callback gets two
arguments:
* A request object, with a single property called "term", which refers to the
value currently in the text input. For example, when the user entered "new yo"
in a city field, the Autocomplete term will equal "new yo".
* A response callback, which expects a single argument to contain 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 (String-Array or Object-Array with label/value/both properties). It's
important when providing a custom source callback to handle errors during
the request. You must always call the response callback even if you
encounter an error. This ensures that the widget always has the correct
state.
---++ Usage
Use =%<nop>JQREQUIRE{"ui::autocomplete"}%= to make use of the library on a wiki page.
Add the =.jqUIAutocomplete= css class to enable autocompletion.
Specify the url to fetch terms in the =autocomplete= html attribute.
Add %SYSTEMWEB%.JQueryMetadata to specify further parameters.
When specifying a local data source for autocompletion terms use the =source=
option within a metadata json object as specified in the examples below.
When specifying a remote source in the =autocomplete= html attribute, the
additional css class =.jqUIAutocomplete= isn't required.
See http://docs.jquery.com/UI/Autocomplete#options for further useful options
that can be specified as metadata.
<verbatim class="tml">
%JQREQUIRE{"ui::autocomplete"}%
<input type='text' class='jqUIAutocomplete {source:["term1", "term2", "..."], delay:500, minLength:3}' />
<input type='text' autocomplete='http://...url to term backend...' />
</verbatim>
---++ Writing autocomplete backends
When fetching autocompletion terms from a remote backend the endpoint has to return a json object
of a specific format interpreted by the library. This can either be a plain array of strings or
an array of json objects with a =label= and an =value= property.
Array of strings:
<verbatim class="js">
[ "term1", "term2", "..." ]
</verbatim>
Array of json objects:
<verbatim class="js">
[
{
label: "display this",
value: "value is this",
},
{
label: "display this",
value: "value is this"
},
...
]
</verbatim>
Custom properties may be specified as part of each json object in the array. Note however to make
use of these you will have to initialize autocompletion using javascript and by
specifying a renderer for items in the autocompltion dropbox yourself like
this:
<verbatim class="html">
<input type="text" id="project" />
<input type="hidden" id="project-id"/>
<img id="project-icon" src="..." />
<div id="project-description"></div>
<literal>
<script type="text/javascript">
var projects = [
{
value: "jquery",
label: "jQuery",
desc: "the write less, do more, JavaScript library",
icon: "jquery_32x32.png"
},
{
value: "jquery-ui",
label: "jQuery UI",
desc: "the official user interface library for jQuery",
icon: "jqueryui_32x32.png"
},
{
value: "sizzlejs",
label: "Sizzle JS",
desc: "a pure-JavaScript CSS selector engine",
icon: "sizzlejs_32x32.png"
}
];
jQuery(function($) {
$("#project").autocomplete({
source: projects,
select: function( event, ui ) {
$("#project").val(ui.item.label);
$("#project-id").val(ui.item.value);
$("#project-description").html(ui.item.desc);
$("#project-icon").attr("src", "images/" + ui.item.icon );
return false;
}
}).data("ui-autocomplete")._renderItem = function(ul, item) {
return $("<li></li>")
.data("item.autocomplete", item)
.append("<a>" + item.label + "<br>" + item.desc + "</a>")
.appendTo( ul );
};
});
</script>
</literal>
</verbatim>
---++ Examples
%JQREQUIRE{"ui::autocomplete"}%
---+++ Using local data
<div class="foswikiFormSteps">
<div class="foswikiFormStep">
*Country:* <input class="foswikiInputField jqUIAutocomplete {source:countryList}" size="60" />
</div>
</div>
<literal>
<script>
var countryList = [
"Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antarctica",
"Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria",
"Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
"Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso",
"Burma", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic",
"Chad", "Chile", "China", "Colombia", "Comoros", "Congo, Democratic Republic",
"Congo, Republic of the", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark",
"Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador",
"Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
"Ethiopia", "Fiji", "Finland", "France", "Gabon", "Gambia", "Georgia",
"Germany", "Ghana", "Greece", "Greenland", "Grenada", "Guatemala", "Guinea",
"Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary",
"Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel",
"Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati",
"Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan", "Laos", "Latvia",
"Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania",
"Luxembourg", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives",
"Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico",
"Micronesia", "Moldova", "Mongolia", "Morocco", "Monaco", "Mozambique",
"Namibia", "Nauru", "Nepal", "Netherlands", "New Zealand", "Nicaragua",
"Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Panama", "Papua New Guinea",
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar",
"Romania", "Russia", "Rwanda", "Samoa", "San Marino", " Sao Tome", "Saudi Arabia",
"Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone",
"Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa",
"Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden",
"Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand",
"Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan",
"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States",
"Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Yemen", "Zambia", "Zimbabwe"
];
</script>
</literal>
---+++ Using remote data
<div class="foswikiFormSteps">
<div class="foswikiFormStep">
<table class='foswikiLayoutTable'>
<tr>
<th>Topic:</th>
<td><input class="foswikiInputField" size="60" autocomplete="%SCRIPTURL{"view"}%/%SYSTEMWEB%/JQueryAjaxHelper?section=topic;skin=text;baseweb=%USERSWEB%" /></td>
</tr>
<tr>
<th>Country:</th>
<td><input class="foswikiInputField" size="60" autocomplete="%SCRIPTURL{"view"}%/%WEB%/%TOPIC%?section=data;skin=text" /></td>
</tr>
</table>
</div>
</div>
This implementation extracts the list of countries from %SYSTEMWEB%.CountryList:
<verbatim class="tml">
%STARTSECTION{"data"}%<noautolink>
%SEARCH{
"^\| [^\*].*%URLPARAM{"term"}%.* \| *$"
web="%SYSTEMWEB%"
topic="CountryList"
type="regex"
multiple="on"
nonoise="on"
casesensitive="off"
header="["
format="\"$pattern(.*?\| ([^\|]*) \|.*)\""
separator=", "
footer="]"
}%
</noautolink>%ENDSECTION{"data"}%
</verbatim>
See http://jqueryui.com/demos/autocomplete/ for more demos.