2
0
mirror of https://github.com/devbridge/jQuery-Autocomplete.git synced 2024-09-20 01:09:03 +00:00
jQuery-Autocomplete/scripts/demo.js

78 lines
2.8 KiB
JavaScript
Raw Normal View History

2012-11-11 03:44:42 +00:00
/*jslint browser: true, white: true, plusplus: true */
/*global $: true */
$(function () {
'use strict';
// Load countries then initialize plugin:
2012-11-11 03:44:42 +00:00
$.ajax({
url: 'content/countries.txt',
dataType: 'json'
}).done(function (source) {
var countriesArray = $.map(source, function (value, key) { return { value: value, data: key }; }),
countries = $.map(source, function (value) { return value; });
// Setup jQuery ajax mock:
$.mockjax({
url: '*',
responseTime: 2000,
response: function (settings) {
var query = settings.data.query,
queryLowerCase = query.toLowerCase(),
2013-06-07 23:37:01 +00:00
re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi'),
suggestions = $.grep(countriesArray, function (country) {
// return country.value.toLowerCase().indexOf(queryLowerCase) === 0;
return re.test(country.value);
}),
response = {
query: query,
suggestions: suggestions
};
this.responseText = JSON.stringify(response);
}
});
// Initialize ajax autocomplete:
$('#autocomplete-ajax').autocomplete({
// serviceUrl: '/autosuggest/service/url',
lookup: countriesArray,
lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
2013-06-07 23:37:01 +00:00
var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
return re.test(suggestion.value);
},
onSelect: function(suggestion) {
$('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
},
onHint: function (hint) {
$('#autocomplete-ajax-x').val(hint);
2013-06-08 00:21:24 +00:00
},
onInvalidateSelection: function() {
$('#selction-ajax').html('You selected: none');
}
});
// Initialize autocomplete with local lookup:
$('#autocomplete').autocomplete({
lookup: countriesArray,
minChars: 0,
2012-11-11 03:44:42 +00:00
onSelect: function (suggestion) {
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
2012-11-11 03:44:42 +00:00
}
});
2013-01-20 21:17:04 +00:00
// Initialize autocomplete with custom appendTo:
$('#autocomplete-custom-append').autocomplete({
lookup: countriesArray,
appendTo: '#suggestions-container'
});
// Initialize autocomplete with custom appendTo:
$('#autocomplete-dynamic').autocomplete({
lookup: countriesArray
});
2013-01-20 21:17:04 +00:00
2012-11-11 03:44:42 +00:00
});
2012-11-11 03:44:42 +00:00
});