Back
Ajax select variants
Searches as you type (min. 1 char). The selected item stays in the list.
Loads initial data even with empty search term when opened.
Automatically fetches and selects the item with ID 3 on initialization.
1000ms debounce and custom response mapping.
These examples demonstrate different Ajax configurations for AvalynxSelect.
Source of "Basic Ajax Search" exampleCopy to clipboard
new AvalynxSelect('#ajax-select-basic', {
ajax: {
url: 'php/result2.get.php',
method: 'GET',
minimumInputLength: 1,
mapRequest: (context) => {
return {
params: {
search: context.term,
perpage: context.length,
page: (context.start / context.length) + 1
}
};
},
mapResponse: (json) => {
if (json && json.data) {
return json.data.map(item => {
return {
value: item.data.id,
text: item.data.company + ' (' + item.data.price + ')'
};
});
}
return [];
}
},
onChange: (value) => {
document.getElementById('result-basic').textContent = 'Selected ID: ' + value;
}
});
new AvalynxSelect('#ajax-select-initial', {
ajax: {
url: 'php/result2.get.php',
method: 'GET',
minimumInputLength: 0,
initialLoad: true,
mapRequest: (context) => {
return {
params: {
search: context.term,
perpage: context.length,
page: (context.start / context.length) + 1
}
};
},
mapResponse: (json) => {
if (json && json.data) {
return json.data.map(item => {
return {
value: item.data.id,
text: item.data.company + ' (' + item.data.price + ')'
};
});
}
return [];
}
},
onChange: (value) => {
document.getElementById('result-initial').textContent = 'Selected ID: ' + value;
}
});
new AvalynxSelect('#ajax-select-default', {
ajax: {
url: 'php/result2.get.php',
method: 'GET',
minimumInputLength: 1,
mapRequest: (context) => {
return {
params: {
search: context.term,
perpage: context.length,
page: (context.start / context.length) + 1
}
};
},
mapResponse: (json) => {
if (json && json.data) {
return json.data.map(item => {
return {
value: item.data.id,
text: item.data.company + ' (' + item.data.price + ')'
};
});
}
return [];
}
},
onChange: (value) => {
document.getElementById('result-default').textContent = 'Selected ID: ' + value;
}
});
Source of "Custom Debounce & Mapping" exampleCopy to clipboard
new AvalynxSelect('#ajax-select-custom', {
ajax: {
url: 'php/result2.get.php',
method: 'GET',
debounce: 1000,
minimumInputLength: 2,
mapRequest: (context) => {
return {
params: {
search: context.term,
perpage: context.length,
page: (context.start / context.length) + 1
}
};
},
mapResponse: (json) => {
if (json && json.data) {
return json.data.map(item => {
return {
value: item.data.id,
text: 'COMPANY: ' + item.data.company.toUpperCase()
};
});
}
return [];
}
},
onChange: (value) => {
document.getElementById('result-custom').textContent = 'Selected ID: ' + value;
}
});
AvalynxSelect on GitHub Avalynx on GitHub