-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
autocomplete.html
395 lines (361 loc) · 13.6 KB
/
autocomplete.html
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<!DOCTYPE html>
<html lang="en">
<head> {{> head }} </head>
<body>
{{> navbar }}
<main>
{{> intro}}
<div class="container">
<div class="row">
<div class="col s12 m8 offset-m1 xl7 offset-xl1">
<div id="introduction" class="section scrollspy">
<p class="caption">
Add an autocomplete dropdown below your input to suggest possible values in your form. You can populate the list of autocomplete
options dynamically as well.
<div class="input-field outlined">
<i class="material-icons prefix">textsms</i>
<input type="text" id="autocomplete-input" class="autocomplete" placeholder=" ">
<label for="autocomplete-input">Autocomplete</label>
<span class="supporting-text">Select a single item</span>
</div>
<div class="col s12">
<p>You can initialize the Autocomplete with isMultiSelect for multiple selections</p>
</div>
<div class="input-field">
<i class="material-icons prefix">textsms</i>
<input type="text" id="autocomplete-input-multi" class="autocomplete-multiple" placeholder=" ">
<label for="autocomplete-input-multi">Multiple Autocomplete</label>
</div>
</p>
<!-- Update Info -->
<div class="col s12">
<p class="red-text"><strong>ATTENTION:</strong> Data-Format has changed from version 1.X.X to 2.0.0! Please update option 'data'.</p>
</div>
<pre style="padding-top: 0px;">
<span class="copyMessage">Copied!</span>
<i class="material-icons copyButton">content_copy</i>
<code class="language-markup copiedText">
<div class="row">
<div class="col s12">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">textsms</i>
<input type="text" id="autocomplete-input" class="autocomplete">
<label for="autocomplete-input">Autocomplete</label>
</div>
</div>
</div>
</div>
</code>
</pre>
</div>
<div id="initialization" class="scrollspy section">
<h3 class="header">Initialization</h3>
<p>The data is an array of option objects, which supports three different attributes:</p>
<p>
<ul class="collection">
<li class="collection-item">
<p style="margin: 0;">
<b>id</b>: This is the only mandatory attribute: it must be a primitive value that can be
converted to string. If "text" is not provided, it will also be used as "option text" as well;
</p>
</li>
<li class="collection-item">
<p style="margin: 0;">
<b>text</b>: This optional attribute is used as "display value" for the current entry.
When provided, it will also be taken into consideration by the standard search function.
</p>
<P style="margin: 0;">
If you trust your data or have properly sanitized your user input, you may use
use HTML by setting the option <code class="language-javascript">allowUnsafeHTML: true</code>;
</P>
</li>
<li class="collection-item">
<p style="margin: 0;">
<b>image</b>: This optional attribute is used to provide a valid image URL to the current option.
This attribute is ignored by the standard search function.
</p>
</li>
</ul>
</p>
<p>
You may also provide additional attributes to an option object but they will not be taken into
consideration by the standard search function. If you want to use them for option filtering, you
must specify a custom function in "<b>onSearch</b>" option.
</p>
<pre style="padding-top: 0px;">
<span class="copyMessage">Copied!</span>
<i class="material-icons copyButton">content_copy</i>
<code class="language-javascript copiedText">
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.autocomplete');
var instances = M.Autocomplete.init(elems, {
// specify options here
minLength: 0, // shows instantly
data: [
{id: 12, text: "Apple"},
{id: 13, text: "Microsoft"},
{id: 42, text: "Google", image: 'http://placehold.it/250x250'}
]
});
});
// With Mutlti Select
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.autocomplete');
var instances = M.Autocomplete.init(elems, {
// specify options here
minLength: 0, // shows instantly
isMultiSelect: true,
data: [
{id: 12, text: "Apple"},
{id: 13, text: "Microsoft"},
{id: 42, text: "Google", image: 'http://placehold.it/250x250'}
]
});
});
// With Mutlti Select and custom search function
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.autocomplete');
var instances = M.Autocomplete.init(elems, {
// specify options here
isMultiSelect: true,
data: [
{id: 12, text: "Apple"},
{id: 13, text: "Microsoft"},
{id: 42, text: "Google", image: 'http://placehold.it/250x250'}
],
// This search function considers every object entry as "search values".
onSearch: (text, autocomplete) => {
const filteredData = autocomplete.options.data.filter(item => {
return Object.keys(item)
.map(key => item[key].toString().toLowerCase().indexOf(text.toLowerCase()) >= 0)
.some(isMatch => isMatch);
});
autocomplete.setMenuItems(filteredData);
}
});
});
</code>
</pre>
</div>
<div id="options" class="scrollspy section">
<h3 class="header">Options</h3>
<table class="striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>data</td>
<td>Array</td>
<td>[]</td>
<td>Array with Data-Objects defining autocomplete options with optional icon (image). The Property <u>id</u> is required which can be string or integer. It is also possible to use the properties <u>text</u> and <u>image</u>.</td>
</tr>
<tr>
<td>isMultiSelect</td>
<td>boolean</td>
<td>false</td>
<td>Flag which can be set if multiple values can be selected. The Result will be an Array.</td>
</tr>
<tr>
<td>maxDropDownHeight</td>
<td>string</td>
<td>'300px'</td>
<td>The height of the Menu which can be set via css-property.</td>
</tr>
<tr>
<td>onAutocomplete</td>
<td>Function</td>
<td>null</td>
<td>Callback for when autocompleted. After a Selection was made this Function is called. This is also called when a default value is set.</td>
</tr>
<tr>
<td>onSearch</td>
<td>Function(text, el)</td>
<td> </td>
<td>Function is called when the input text is altered and data can also be loaded asynchronously. If the results are collected the items in the list can be updated via the function setMenuItems(collectedItems).</td>
</tr>
<tr>
<td>minLength</td>
<td>Number</td>
<td>1</td>
<td>Minimum number of characters before autocomplete starts. When set to 0, the Options are shown on click or focus.</td>
</tr>
<tr>
<td>dropdownOptions</td>
<td>Object</td>
<td>{}</td>
<td>Pass options object for the dropdown initialization.</td>
</tr>
<tr>
<td>allowUnsafeHTML</td>
<td>Boolean</td>
<td>false</td>
<td>If true will render the key from each item directly as HTML. User input MUST be properly sanitized first.</td>
</tr>
</tbody>
</table>
<h5 class="method-header">
onSearch
</h5>
<p>This is the default Search Function. You can write your own search function by passing in a function with the same
parameters. You can load asynchronously data in this function or filter an existing list or dictionary. When the data is loaded
you can use setMenuItems() to update the content of the Menu.
</p>
<pre style="padding-top: 0px;">
<span class="copyMessage">Copied!</span>
<i class="material-icons copyButton">content_copy</i>
<code class="language-javascript copiedText">
onSearch: (text, autocomplete) => {
const normSearch = text.toLocaleLowerCase();
autocomplete.setMenuItems(
autocomplete.options.data.filter((option) =>
option.id.toString().toLocaleLowerCase().includes(normSearch)
|| option.text?.toLocaleLowerCase().includes(normSearch)
)
);
}
</code>
</pre>
<p>To disable sorting and use the values as they appear in the data object, use a falsy value.</p>
</div>
<div id="methods" class="scrollspy section">
<h3 class="header">Methods</h3>
<blockquote>
<p>All the methods are called on the plugin instance. You can get the plugin instance like this: </p>
<pre style="padding-top: 0px;">
<span class="copyMessage">Copied!</span>
<i class="material-icons copyButton">content_copy</i>
<code class="language-javascript copiedText">
var instance = M.Autocomplete.getInstance(elem);
instance.open();
</code>
</pre>
</blockquote>
<h5 class="method-header">
.open();
</h5>
<p>Open autocomplete dropdown.</p>
<pre><code class="language-javascript col s12">
instance.open();
</code></pre>
<h5 class="method-header">
.close();
</h5>
<p>Close autocomplete dropdown.</p>
<pre><code class="language-javascript col s12">
instance.close();
</code></pre>
<h5 class="method-header">
.selectOption(id);
</h5>
<p>Select a specific autocomplete option via id-property.</p>
<h6>Arguments</h6>
<p>
<b>id:</b> The id of a data-entry.</p>
<pre><code class="language-javascript col s12">
instance.selectOption(42);
</code></pre>
<h5 class="method-header">
.setMenuItems(items);
</h5>
<p>Updates the visible or selectable items shown in the menu.</p>
<h6>Arguments</h6>
<p>
<b>items:</b> .</p>
<pre><code class="language-javascript col s12">
instance.setMenuItems([
{id: "Test"},
{id: 12, text: "Apple"},
{id: 13, text: "Microsoft"},
{id: 42, text: "Google", image: 'http://placehold.it/250x250'}
]);
</code></pre>
<h5 class="method-header">
.destroy();
</h5>
<p>Destroy plugin instance and teardown</p>
<pre><code class="language-javascript col s12">
instance.destroy();
</code></pre>
</div>
<div id="properties" class="scrollspy section">
<h3 class="header">Properties</h3>
<table class="striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>el</td>
<td>Element</td>
<td>The DOM element the plugin was initialized with.</td>
</tr>
<tr>
<td>options</td>
<td>Object</td>
<td>The options the instance was initialized with.</td>
</tr>
<tr>
<td>isOpen</td>
<td>Boolean</td>
<td>If the autocomplete is open.</td>
</tr>
<tr>
<td>count</td>
<td>Number</td>
<td>Number of matching autocomplete options.</td>
</tr>
<tr>
<td>activeIndex</td>
<td>Integer</td>
<td>Index of the current selected option.</td>
</tr>
<tr>
<td>dropdown</td>
<td>Dropdown</td>
<td>Instance of the dropdown plugin for this autocomplete.</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col hide-on-small-only m3 xl3">
<div class="toc-wrapper">
<div style="height: 1px;">
<ul class="section table-of-contents">
<li>
<a href="#introduction">Introduction</a>
</li>
<li>
<a href="#initialization">Initialization</a>
</li>
<li>
<a href="#options">Options</a>
</li>
<li>
<a href="#methods">Methods</a>
</li>
<li>
<a href="#properties">Properties</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</main>
{{> footer }}
<script type="module" src="/src/main.ts"></script>
</body>
</html>