2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-11-29 08:14:06 +00:00

Merge pull request #25 from nilsga/master

Allow for configurable name of query request parameter and custom response format.
This commit is contained in:
Tomas Kirda 2013-01-16 12:21:20 -08:00
commit d93af49a5e
2 changed files with 26 additions and 6 deletions

View File

@ -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.
* `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.
* `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
Html:
@ -103,6 +104,21 @@ supply just a string array for suggestions:
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
field, otherwise suggestions will not be displayed.

View File

@ -90,6 +90,10 @@
tabDisabled: false,
lookupFilter: function (suggestion, originalQuery, queryLowerCase) {
return suggestion.value.toLowerCase().indexOf(queryLowerCase) !== -1;
},
paramName: 'query',
transformResult: function(response) {
return response.suggestions;
}
};
@ -379,7 +383,7 @@
that.suggest();
} else if (!that.isBadQuery(q)) {
options.onSearchStart.call(that.element, q);
options.params.query = q;
options.params[options.paramName] = q;
$.ajax({
url: options.serviceUrl,
data: options.params,
@ -454,18 +458,18 @@
var that = this,
response = $.parseJSON(text);
response.suggestions = that.verifySuggestionsFormat(response.suggestions);
response.suggestions = that.verifySuggestionsFormat(that.options.transformResult(response));
// Cache results if cache is not disabled:
if (!that.options.noCache) {
that.cachedResponse[response.query] = response;
that.cachedResponse[response[that.options.paramName]] = response;
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:
if (response.query === that.getQuery(that.currentValue)) {
if (response[that.options.paramName] === that.getQuery(that.currentValue)) {
that.suggestions = response.suggestions;
that.suggest();
}