mirror of
https://github.com/devbridge/jQuery-Autocomplete.git
synced 2024-11-09 23:01:00 +00:00
Added support for custom query and search result format
This commit is contained in:
parent
ffa963663a
commit
b92ac02ba7
18
readme.md
18
readme.md
@ -33,7 +33,8 @@ The standard jquery.autocomplete.js file is around 2.7KB when minified via Closu
|
|||||||
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
|
* `onSearchStart`: `function (query) {}` called before ajax request. `this` is bound to input element.
|
||||||
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
|
* `onSearchComplete`: `function (query) {}` called after ajax response is processed. `this` is bound to input element.
|
||||||
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
|
* `tabDisabled`: Default `false`. Set to true to leave the cursor in the input field after the user tabs to select a suggestion.
|
||||||
|
* `paramName`: Default `query`. The name of the request parameter that contains the query.
|
||||||
|
* `transformResult`: `function(response) {}` called after the result of the query is ready. Converts the result into response.suggestions format.
|
||||||
##Usage
|
##Usage
|
||||||
|
|
||||||
Html:
|
Html:
|
||||||
@ -103,6 +104,21 @@ supply just a string array for suggestions:
|
|||||||
suggestions: ["United Arab Emirates", "United Kingdom", "United States"]
|
suggestions: ["United Arab Emirates", "United Kingdom", "United States"]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
## Non standard query/results
|
||||||
|
|
||||||
|
If your ajax service expects the query in a different format, and returns data in a different format then the standard response,
|
||||||
|
you can supply the "paramName" and "transformResult" options:
|
||||||
|
|
||||||
|
$('#autocomplete').autocomplere({
|
||||||
|
paramName: 'searchString',
|
||||||
|
transformResult: function(response) {
|
||||||
|
return $.map(response.myData, function(dataItem) {
|
||||||
|
return {value: dataItem.valueField, data: dataItem.dataField};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
Important: query value must match original value in the input
|
Important: query value must match original value in the input
|
||||||
field, otherwise suggestions will not be displayed.
|
field, otherwise suggestions will not be displayed.
|
||||||
|
|
||||||
|
@ -90,6 +90,10 @@
|
|||||||
tabDisabled: false,
|
tabDisabled: false,
|
||||||
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
|
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
|
||||||
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
|
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
|
||||||
|
},
|
||||||
|
paramName: 'query',
|
||||||
|
transformResult: function(response) {
|
||||||
|
return response.suggestions;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -379,7 +383,7 @@
|
|||||||
that.suggest();
|
that.suggest();
|
||||||
} else if (!that.isBadQuery(q)) {
|
} else if (!that.isBadQuery(q)) {
|
||||||
options.onSearchStart.call(that.element, q);
|
options.onSearchStart.call(that.element, q);
|
||||||
options.params.query = q;
|
options.params[options.paramName] = q;
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: options.serviceUrl,
|
url: options.serviceUrl,
|
||||||
data: options.params,
|
data: options.params,
|
||||||
@ -454,18 +458,18 @@
|
|||||||
var that = this,
|
var that = this,
|
||||||
response = $.parseJSON(text);
|
response = $.parseJSON(text);
|
||||||
|
|
||||||
response.suggestions = that.verifySuggestionsFormat(response.suggestions);
|
response.suggestions = that.verifySuggestionsFormat(that.options.transformResult(response));
|
||||||
|
|
||||||
// Cache results if cache is not disabled:
|
// Cache results if cache is not disabled:
|
||||||
if (!that.options.noCache) {
|
if (!that.options.noCache) {
|
||||||
that.cachedResponse[response.query] = response;
|
that.cachedResponse[response[that.options.paramName]] = response;
|
||||||
if (response.suggestions.length === 0) {
|
if (response.suggestions.length === 0) {
|
||||||
that.badQueries.push(response.query);
|
that.badQueries.push(response[that.options.paramName]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display suggestions only if returned query matches current value:
|
// Display suggestions only if returned query matches current value:
|
||||||
if (response.query === that.getQuery(that.currentValue)) {
|
if (response[that.options.paramName] === that.getQuery(that.currentValue)) {
|
||||||
that.suggestions = response.suggestions;
|
that.suggestions = response.suggestions;
|
||||||
that.suggest();
|
that.suggest();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user