AJAX
DotSelect has built-in support for loading options from a remote server via AJAX. Configure everything through data attributes — no JavaScript needed.
Basic AJAX Select
Point data-ajax-url to your API endpoint:
When the user types in the search box, DotSelect replaces {@term} with the search query and fetches results from the URL.
Token System
DotSelect uses a token system to build dynamic URLs. Tokens are placeholders wrapped in {@...} that get replaced at request time.
| Token | Description | Example |
|---|---|---|
{@term} | The current search query typed by the user | ?q={@term} |
{@page} | The current page number (for pagination) | ?page={@page} |
{@selected} | Comma-separated list of currently selected values | ?exclude={@selected} |
{@val} | The current value of the select | ?current={@val} |
{@pagination_limit} | The number of results per page | ?limit={@pagination_limit} |
Token Usage Examples
Field Mapping
By default, DotSelect expects each result object to have id and text fields. If your API returns different field names, use mapping attributes:
| Attribute | Description | Default |
|---|---|---|
data-option-id | The field name to use as the option value | id |
data-option-text | The field name to use as the option label | text |
data-option-disabled | The field name to determine if an option is disabled | disabled |
data-option-group | The field name for grouping options | group |
Nested Field Paths
You can use dot notation for nested fields:
Response Format
DotSelect expects the API response to be JSON. By default, it looks for an array of objects at the root level or inside a data key:
// Root-level array
[
{ "id": 1, "text": "Alice" },
{ "id": 2, "text": "Bob" }
]
// Nested under "data" key
{
"data": [
{ "id": 1, "text": "Alice" },
{ "id": 2, "text": "Bob" }
]
}If your results are nested under a different key, specify it with data-ajax-data-key:
AJAX Configuration Attributes
| Attribute | Description | Default |
|---|---|---|
data-ajax-url | The URL to fetch data from (supports tokens) | — |
data-ajax-method | HTTP method | GET |
data-ajax-delay | Debounce delay in ms before sending request | 300 |
data-ajax-min-chars | Minimum characters before triggering search | 1 |
data-ajax-data-key | Key path to the results array in the response | data |
data-ajax-cache | Cache AJAX results | true |
data-ajax-headers | JSON string of custom request headers | {} |
Custom Headers
Send authentication tokens or custom headers with your requests:
Templates
Customize how options and selected items are displayed with template attributes. Templates use token syntax to reference fields from the data:
Available Template Attributes
| Attribute | Description |
|---|---|
data-template-option | Template for each option in the dropdown |
data-template-item | Template for selected items (displayed in the control) |
data-template-no-results | Template shown when no results are found |
data-template-loading | Template shown while loading AJAX results |
TIP
Template tokens correspond to fields in your API response. Any field returned by the API can be used as a {@field_name} token in templates.
Pagination
Enable infinite-scroll pagination for large datasets:
DotSelect automatically detects whether more pages are available. Your API response should include pagination metadata:
{
"data": [
{ "id": 1, "text": "Alice" },
{ "id": 2, "text": "Bob" }
],
"pagination": {
"more": true
}
}Or specify a custom key for the "has more" flag:
| Attribute | Description | Default |
|---|---|---|
data-pagination | Enable pagination | false |
data-pagination-limit | Number of results per page | 20 |
data-pagination-more-key | Key path in response indicating more pages exist | pagination.more |
Resolve Mode (Pre-selected AJAX Values)
When using AJAX with pre-selected values, DotSelect needs to resolve those values into display text. Use data-selected to specify pre-selected values that should be fetched from the server:
On initialization, DotSelect calls the resolve URL with the pre-selected IDs to fetch their display text.
WARNING
Without data-ajax-resolve-url, pre-selected values in AJAX mode will show the raw value instead of the display text. Always provide a resolve endpoint when using data-selected with AJAX.
Resolve Configuration
| Attribute | Description | Default |
|---|---|---|
data-selected | Comma-separated IDs to pre-select | — |
data-ajax-resolve-url | URL to resolve selected IDs to full objects | — |
Full AJAX Example
A complete example with search, pagination, templates, and pre-selected values: