Remote data · live API
Search, page and load more from a real API.
Remote selects have always been supported through a fetchOptions function. 2.4.2
also makes them declarative: give a select data-zidan-fetch-url and the plugin
builds the request, the debounce, the abort handling, the LRU cache and the
Load more footer for you. The first two cards below hit HTTP endpoints — nothing is
faked in memory except the JSON file that stands in for a slow server.
1. One attribute: data-zidan-fetch-url
This combobox is a plain <select> with data attributes only. Every keystroke
debounces into GET https://dummyjson.com/products/search?q=…&limit=8&skip=…;
aborting the previous request, caching pages and showing Load more while the
response still has rows left.
open the list to see its async state
<select id="products"
data-zidan-select="combobox"
data-zidan-fetch-url="https://dummyjson.com/products/search"
data-zidan-fetch-items-path="products"
data-zidan-fetch-total-path="total"
data-zidan-fetch-skip-param="skip"
data-zidan-fetch-page-size="8"
data-zidan-fetch-page-size-param="limit"></select>
2. Your own fetchOptions
When the response shape or the URL building is yours, return the promise yourself. This card
fetches a static JSON file over HTTP and simulates a slow server so you can watch
abort, the loading state and the cache. The method receives
(query, signal, { page, pageSize, append }) and resolves to
{ items, hasMore } or a plain array.
type to search — the fake server waits 300 ms
const wait = (ms, signal) => new Promise((resolve, reject) => {
const timer = setTimeout(resolve, ms);
signal.addEventListener('abort', () => {
clearTimeout(timer);
reject(new DOMException('aborted', 'AbortError'));
});
});
zidanSelect('#country', {
mode: 'combobox',
fetchPageSize: 5,
minChars: 0,
debounce: 200,
fetchOptions: async (query, signal, { page, pageSize }) => {
await wait(300, signal);
const res = await fetch('demo-data/countries.json', { signal });
if (!res.ok) throw new Error('countries.json responded ' + res.status);
const rows = await res.json();
const needle = String(query || '').trim().toLowerCase();
const hits = needle
? rows.filter(row => row.name.toLowerCase().includes(needle))
: rows;
const start = page * pageSize;
return {
items: hits.slice(start, start + pageSize)
.map(row => ({ value: row.code, label: row.name })),
hasMore: start + pageSize < hits.length
};
}
});
3. Errors stay visible — and retry is explicit
Simulate one rejected request, then retry it. An initial failure shows an error state and
getAsyncState().error keeps the message; refreshOptions() clears
the cache and retries. If an appended page fails, the existing rows stay visible and the
Load more footer becomes the retry button.
open the list, then simulate a failed request
let failNextRequest = false;
zidanSelect('#retry-country', {
mode: 'combobox',
fetchPageSize: 5,
fetchOptions: async (query, signal, { page, pageSize }) => {
await new Promise(resolve => setTimeout(resolve, 300));
if (failNextRequest) {
failNextRequest = false;
throw new Error('Demo server is unavailable. Try again.');
}
const res = await fetch('demo-data/countries.json', { signal });
if (!res.ok) throw new Error('Request failed: ' + res.status);
const rows = await res.json();
const hits = rows.filter(row =>
row.name.toLowerCase().includes(query.toLowerCase()));
const start = page * pageSize;
return {
items: hits.slice(start, start + pageSize),
hasMore: start + pageSize < hits.length
};
}
});
// The demo buttons set failNextRequest = true and call these methods.
instance.refreshOptions(); // retry an initial failure
instance.loadMore(); // retry a failed appended page
4. The fetch contract
Return shape
An array, or { items, hasMore }, or { items, total }. With
fetchUrl the plugin reads fetchItemsPath,
fetchTotalPath or fetchHasMorePath; it also understands
items, data, results, options,
rows, products and records without a path.
Item mapping
Plain strings work. Objects use value / label; otherwise
id, slug or code for the value and
name, title or full_name for the label — which is
why the dummyjson title field renders without a mapping function.
Behaviour you inherit
debounce, minChars, an AbortSignal per request,
an LRU cacheResults, loadMore(),
refreshOptions(), clearCache(),
getAsyncState(), the zidan:load-more event, and a failed
appended-page footer that retries instead of discarding the rows already loaded.