Skip to content

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.

TokenDescriptionExample
{@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:

AttributeDescriptionDefault
data-option-idThe field name to use as the option valueid
data-option-textThe field name to use as the option labeltext
data-option-disabledThe field name to determine if an option is disableddisabled
data-option-groupThe field name for grouping optionsgroup

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:

json
// 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

AttributeDescriptionDefault
data-ajax-urlThe URL to fetch data from (supports tokens)
data-ajax-methodHTTP methodGET
data-ajax-delayDebounce delay in ms before sending request300
data-ajax-min-charsMinimum characters before triggering search1
data-ajax-data-keyKey path to the results array in the responsedata
data-ajax-cacheCache AJAX resultstrue
data-ajax-headersJSON 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

AttributeDescription
data-template-optionTemplate for each option in the dropdown
data-template-itemTemplate for selected items (displayed in the control)
data-template-no-resultsTemplate shown when no results are found
data-template-loadingTemplate 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:

json
{
  "data": [
    { "id": 1, "text": "Alice" },
    { "id": 2, "text": "Bob" }
  ],
  "pagination": {
    "more": true
  }
}

Or specify a custom key for the "has more" flag:

AttributeDescriptionDefault
data-paginationEnable paginationfalse
data-pagination-limitNumber of results per page20
data-pagination-more-keyKey path in response indicating more pages existpagination.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

AttributeDescriptionDefault
data-selectedComma-separated IDs to pre-select
data-ajax-resolve-urlURL to resolve selected IDs to full objects

Full AJAX Example

A complete example with search, pagination, templates, and pre-selected values:

MIT Licensed