Typeahead ajax autocomplete is not working -
i trying make typeahead autocomplete ajax data, it's not working.
this code.
html
<input type="search" class="typeahead" id="order_address" autocomplete="off">
javascript
$(document).ready(function() {
var suggestions = new bloodhound({ remote: { url: 'https://autocomplete.geocoder.api.here.com/6.2/suggest.json?app_id=...app_code=...&country=usa&mapview=41.382995,-74.301616;41.305715,-74.092278&query=%query%', wildcard: '%query%', filter: function (response) { return response.suggestions; } }, datumtokenizer: function(suggestions) { return bloodhound.tokenizers.whitespace(suggestions); }, querytokenizer: bloodhound.tokenizers.whitespace, }); $('#order_address').typeahead({ hint: true, highlight: true, minlength: 1 }, { name: 'suggestions', displaykey: function(suggestions) { return suggestions.label; }, source: suggestions.ttadapter() });
});
when inspecting browsers network data, it's correctly getting suggestion data such below.
{"suggestions":[{"label":"united states of america, ny, cornwall, angola rd","language":"en","countrycode":"usa","locationid":"nt_7cpok364jilgh4ksucyjpc","address":{"country":"united states of america","state":"ny","county":"orange","city":"cornwall","street":"angola rd","postalcode":"12518"},"distance":14896,"matchlevel":"street"},{"label":"united states of america, ny, garrison, albany post rd","language":"en","countrycode":"usa","locationid":"nt_dm6n2rqmjz1yvbjms6myga","address":{"country":"united states of america","state":"ny","county":"putnam","city":"garrison","district":"garrison","street":"albany post rd","postalcode":"10524"},"distance":23981,"matchlevel":"street"},{"label":"united states of america, ny, montrose, albany post rd","language":"en","countrycode":"usa","locationid":"nt_nnt..hu2z5yxhvu4upgxwa","address":{"country":"united states of america","state":"ny","county":"westchester","city":"montrose","street":"albany post rd","postalcode":"10548"},"distance":24394,"matchlevel":"street"},{"label":"united states of america, ny, croton-on-hudson, albany post rd","language":"en","countrycode":"usa","locationid":"nt_foknpgy5gjxskp195bkloa","address":{"country":"united states of america","state":"ny","county":"westchester","city":"croton-on-hudson","street":"albany post rd","postalcode":"10520"},"distance":26329,"matchlevel":"street"}]}
but it's not working autocomplete.
what can now?
you need include key want use filter objects suggestions
array in datumtokenizer
method of bloodhound
instance. passing in suggestions argument bloodhound.tokenizers.whitespace
work if suggestions array of strings — tokenizer expects arguments resolve string tokens can matched against.
if wanted match against labels inside suggestions arrays, need change datumtokenizer
function return bloodhound.tokenizers.whitespace(suggestions.label)
.
or if wanted check against several properties, need return array of tokenizers [bloodhound.tokenizers.whitespace(suggestions.label), bloodhound.tokenizers.whitespace(suggestions.language)]
.
see snippet below of examples of matching against 1 , 2 properties of array.
$(document).ready(function() { const data = {"suggestions":[{"label":"united states of america, ny, cornwall, angola rd","language":"en","countrycode":"usa","locationid":"nt_7cpok364jilgh4ksucyjpc","address":{"country":"united states of america","state":"ny","county":"orange","city":"cornwall","street":"angola rd","postalcode":"12518"},"distance":14896,"matchlevel":"street"},{"label":"united states of america, ny, garrison, albany post rd","language":"en","countrycode":"usa","locationid":"nt_dm6n2rqmjz1yvbjms6myga","address":{"country":"united states of america","state":"ny","county":"putnam","city":"garrison","district":"garrison","street":"albany post rd","postalcode":"10524"},"distance":23981,"matchlevel":"street"},{"label":"united states of america, ny, montrose, albany post rd","language":"en","countrycode":"usa","locationid":"nt_nnt..hu2z5yxhvu4upgxwa","address":{"country":"united states of america","state":"ny","county":"westchester","city":"montrose","street":"albany post rd","postalcode":"10548"},"distance":24394,"matchlevel":"street"},{"label":"united states of america, ny, croton-on-hudson, albany post rd","language":"en","countrycode":"usa","locationid":"nt_foknpgy5gjxskp195bkloa","address":{"country":"united states of america","state":"ny","county":"westchester","city":"croton-on-hudson","street":"albany post rd","postalcode":"10520"},"distance":26329,"matchlevel":"street"}]}; var suggestions = new bloodhound({ local: data.suggestions, datumtokenizer: function(suggestions) { return bloodhound.tokenizers.whitespace(suggestions.label); }, querytokenizer: bloodhound.tokenizers.whitespace, }); var suggestions2 = new bloodhound({ local: data.suggestions, datumtokenizer: function(s) { return ['countrycode','matchlevel'].reduce(function(p,c) { return p.concat(bloodhound.tokenizers.whitespace(s[c])) }, []); }, querytokenizer: bloodhound.tokenizers.whitespace, }); $('#order_address').typeahead({ hint: true, highlight: true, minlength: 1 }, { name: 'suggestions', displaykey: function(suggestions) { return suggestions.label; }, source: suggestions.ttadapter() }); $('#order_address2').typeahead({ hint: true, highlight: true, minlength: 1 }, { name: 'suggestions2', displaykey: function(suggestions) { return suggestions.label; }, source: suggestions2.ttadapter() }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js"></script> <p>matches against label only</p> <input type="search" class="typeahead" id="order_address" autocomplete="off"> <p>matches against countrycode , matchlevel</p> <input type="search" class="typeahead" id="order_address2" autocomplete="off">
Comments
Post a Comment