You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- ✅ **Dynamic Content** - Works with AJAX-loaded selects
15
16
- ✅ **Mutation Observer** - Syncs with programmatic changes to original select
@@ -70,6 +71,187 @@ Free type means that you allow the user to add an option that is not in the list
70
71
</select>
71
72
```
72
73
74
+
## AJAX Loading
75
+
76
+
The autocomplete component can load options dynamically from an API endpoint using the `data-ajax-url` attribute. This is ideal for large datasets, search-as-you-type functionality, or data that changes frequently.
<optionvalue="">Search and select multiple news...</option>
217
+
</select>
218
+
```
219
+
220
+
Behavior:
221
+
222
+
- Selected items appear as tags
223
+
- Tags are excluded from API results (no duplicates)
224
+
- Search resets to page 1
225
+
- Pagination continues to work as user scrolls
226
+
227
+
### AJAX Error Handling
228
+
229
+
The component does not currently implement explicit error handling. If the API request fails:
230
+
231
+
- Console errors will appear
232
+
- Options list will remain empty or show previous results
233
+
- No user-facing error message displayed
234
+
235
+
**Best practices:**
236
+
237
+
- Ensure API endpoint is reliable
238
+
- Test API response format carefully
239
+
- Monitor console for network errors
240
+
- Consider adding server-side logging
241
+
242
+
::: warning API Format Required
243
+
The AJAX feature **requires** the exact JSON format shown above. Incorrect format will cause the component to fail silently or throw JavaScript errors.
244
+
:::
245
+
246
+
::: tip Combining with Static Options
247
+
You can combine AJAX options with static `<option>` elements. The component loads both:
248
+
249
+
1. First, AJAX options from the API
250
+
2. Then, static options from the select element
251
+
252
+
This is useful for having a "Other" or "None" option alongside dynamic data.
253
+
:::
254
+
73
255
## Required Attributes
74
256
75
257
| Attribute | Required | Description |
@@ -83,6 +265,7 @@ Free type means that you allow the user to add an option that is not in the list
83
265
|`multiple`| Native HTML attribute for multiple selection. Component adds tag-style UI for selected items. |
84
266
|`free-type`| Allows users to enter custom values not in the option list. Creates a new option dynamically. |
85
267
|`disabled`| Native HTML attribute. Component observes changes and updates UI accordingly via MutationObserver. |
268
+
|`data-ajax-url`| URL to fetch options from via AJAX. Response must match the API format (see AJAX Loading section). |
86
269
|`data-autocomplete-reference`| CSS selector for a different element to append the dropdown to (useful for modals or overflow containers). |
87
270
88
271
::: tip Free Type Usage
@@ -97,17 +280,20 @@ With `free-type`, users can type any value and it will be added to the select as
97
280
2.**Validates** element is a `<select>` (others are skipped)
98
281
3.**Removes**`data-autocomplete` attribute
99
282
4.**Adds**`data-autocomplete-init` with unique path identifier
100
-
5.**Creates** UI structure:
283
+
5.**Detects**`data-ajax-url` attribute for AJAX mode
284
+
6.**Creates** UI structure:
101
285
- Wrapper div (`.autocomplete`)
102
286
- Input field for searching
103
287
- Dropdown list (hidden by default)
104
288
- Status div for screen readers
105
289
- Dropdown icon button
106
-
6.**Hides** original `<select>` (sets `aria-hidden="true"`, `tabindex="-1"`, adds `hidden` class)
107
-
7.**Copies** classes from `<select>` to new autocomplete element
108
-
8.**Sets up** MutationObserver to watch for changes to original select
109
-
9.**Parses** options and selected values
110
-
10.**Supports** dynamic content via `DOMHelper.onDynamicContent`
290
+
7.**Hides** original `<select>` (sets `aria-hidden="true"`, `tabindex="-1"`, adds `hidden` class)
291
+
8.**Copies** classes from `<select>` to new autocomplete element
292
+
9.**Sets up** MutationObserver to watch for changes to original select
293
+
10.**Loads** options (from AJAX if URL provided, or from static options)
294
+
11.**Sets up** scroll listener for infinite pagination (AJAX mode only)
295
+
12.**Parses** selected values from both AJAX data and static options
296
+
13.**Supports** dynamic content via `DOMHelper.onDynamicContent`
111
297
112
298
### User Interaction Flow
113
299
@@ -204,12 +390,14 @@ The autocomplete wrapper automatically receives all classes from the original `<
204
390
205
391
## Search Behavior
206
392
393
+
### Static Options
394
+
207
395
The component filters options using **accent-insensitive** matching:
208
396
209
397
```javascript
210
398
// Both of these will match "José":
211
-
'jose'; // User types without accent
212
-
'José'; // Option in list
399
+
"jose"; // User types without accent
400
+
"José"; // Option in list
213
401
214
402
// Normalizes: é → e, ñ → n, ü → u, etc.
215
403
```
@@ -221,6 +409,25 @@ The component filters options using **accent-insensitive** matching:
221
409
3. Matches against both original and normalized option text
222
410
4. Shows all matching options
223
411
412
+
### AJAX Options
413
+
414
+
When `data-ajax-url` is set, search behavior changes:
415
+
416
+
1. User types in input field
417
+
2. Component sends `?q={searchTerm}&page=1` to API endpoint
418
+
3. API performs server-side search and returns matching results
419
+
4. Component replaces current options with search results
420
+
5. Selected options are preserved and merged with results
421
+
6. Pagination resets to page 1 on each new search
422
+
7. Infinite scroll works with search results
423
+
424
+
**AJAX search characteristics:**
425
+
426
+
- Server-side filtering (implement in your API)
427
+
- Resets to page 1 on every keystroke
428
+
- Tracks search term separately for pagination
429
+
- Empty search term loads all results (page 1)
430
+
224
431
## Accessibility
225
432
226
433
### ARIA Implementation
@@ -282,12 +489,12 @@ A **MutationObserver** watches the original `<select>` for:
282
489
283
490
```javascript
284
491
// These changes are automatically detected:
285
-
selectElement.value='5';
492
+
selectElement.value="5";
286
493
selectElement.selectedIndex=2;
287
494
selectElement.options[0].selected=true;
288
495
289
496
// Trigger jschange event to update autocomplete UI:
0 commit comments