|
51 | 51 |
|
52 | 52 | <div id="initialization" class="scrollspy section">
|
53 | 53 | <h3 class="header">Initialization</h3>
|
54 |
| - <p>The data is a json object where the key is the matching string and the value is an optional image url.</p> |
55 |
| - <p>The key must be a text string. If you trust your data, or have properly sanitized your user input, you may |
56 |
| - use HTML by setting the option <code class="language-javascript">allowUnsafeHTML: true</code>.</p> |
| 54 | + <p>The data is an array of option objects, which supports three different attributes:</p> |
| 55 | + <p> |
| 56 | + <ul class="collection"> |
| 57 | + <li class="collection-item"> |
| 58 | + <p style="margin: 0;"> |
| 59 | + <b>id</b>: This is the only mandatory attribute: it must be a primitive value that can be |
| 60 | + converted to string. If "text" is not provided, it will also be used as "option text" as well; |
| 61 | + </p> |
| 62 | + </li> |
| 63 | + <li class="collection-item"> |
| 64 | + <p style="margin: 0;"> |
| 65 | + <b>text</b>: This optional attribute is used as "display value" for the current entry. |
| 66 | + When provided, it will also be taken into consideration by the standard search function. |
| 67 | + </p> |
| 68 | + <P style="margin: 0;"> |
| 69 | + If you trust your data or have properly sanitized your user input, you may use |
| 70 | + use HTML by setting the option <code class="language-javascript">allowUnsafeHTML: true</code>; |
| 71 | + </P> |
| 72 | + </li> |
| 73 | + <li class="collection-item"> |
| 74 | + <p style="margin: 0;"> |
| 75 | + <b>image</b>: This optional attribute is used to provide a valid image URL to the current option. |
| 76 | + This attribute is ignored by the standard search function. |
| 77 | + </p> |
| 78 | + </li> |
| 79 | + </ul> |
| 80 | + </p> |
| 81 | + <p> |
| 82 | + You may also provide additional attributes to an option object but they will not be taken into |
| 83 | + consideration by the standard search function. If you want to use them for option filtering, you |
| 84 | + must specify a custom function in "<b>onSearch</b>" option. |
| 85 | + </p> |
57 | 86 | <pre style="padding-top: 0px;">
|
58 | 87 | <span class="copyMessage">Copied!</span>
|
59 | 88 | <i class="material-icons copyButton">content_copy</i>
|
@@ -99,7 +128,8 @@ <h3 class="header">Initialization</h3>
|
99 | 128 | {id: 13, text: "Microsoft"},
|
100 | 129 | {id: 42, text: "Google", image: 'http://placehold.it/250x250'}
|
101 | 130 | ],
|
102 |
| - onSearch: function(text, autocomplete) { |
| 131 | + // This search function considers every object entry as "search values". |
| 132 | + onSearch: (text, autocomplete) => { |
103 | 133 | const filteredData = autocomplete.options.data.filter(item => {
|
104 | 134 | return Object.keys(item)
|
105 | 135 | .map(key => item[key].toString().toLowerCase().indexOf(text.toLowerCase()) >= 0)
|
@@ -188,13 +218,14 @@ <h5 class="method-header">
|
188 | 218 | <span class="copyMessage">Copied!</span>
|
189 | 219 | <i class="material-icons copyButton">content_copy</i>
|
190 | 220 | <code class="language-javascript copiedText">
|
191 |
| -onSearch: function(text, autocomplete) { |
192 |
| - const filteredData = autocomplete.options.data.filter(item => { |
193 |
| - return Object.keys(item) |
194 |
| - .map(key => item[key].toString().toLowerCase().indexOf(text.toLowerCase()) >= 0) |
195 |
| - .some(isMatch => isMatch);<
7D55
/div> |
196 |
| - }); |
197 |
| - autocomplete.setMenuItems(filteredData); |
| 221 | +onSearch: (text, autocomplete) => { |
| 222 | + const normSearch = text.toLocaleLowerCase(); |
| 223 | + autocomplete.setMenuItems( |
| 224 | + autocomplete.options.data.filter((option) => |
| 225 | + option.id.toString().toLocaleLowerCase().includes(normSearch) |
| 226 | + || option.text?.toLocaleLowerCase().includes(normSearch) |
| 227 | + ) |
| 228 | + ); |
198 | 229 | }
|
199 | 230 | </code>
|
200 | 231 | </pre>
|
|