Typeahead bootstrap3: how to pass a parameter to the remote?

I am trying to use typehhed twitter to download https://github.com/twitter/typeahead.js

I need to dynamically change the dynamic mode according to what types of users and the ANOTHER parameter. (The goal is to get the cities of the country)

attempt with country = "en"; autofill attempt does not affect this .remote = ".."; does not work.

Any idea?

<script> var country="fr"; var autocompleter = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: 'ajax.php?action=getVilles&country='+country+'&ville=%QUERY' }); autocompleter.initialize(); $('#city').typeahead(null, { name: 'city', displayKey: 'city', source: autocompleter.ttAdapter() }); </script> 
+5
source share
1 answer

This is what I did:

 var tagStudies = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: "autocomplete/study", replace: function(url, uriEncodedQuery) { study = $('#study').val(); ssp = $("#social-security").val(); return url + '?q=' + uriEncodedQuery + '&s=' + study + '&ssp=' + ssp }, filter: function (parsedResponse) { return parsedResponse.studies; } } }); 

Take a look at the replacement feature. The first parameter is url, and the second is what the user types. I did concatenation to pass the request and 2 additional parameters. If you copy the code, be sure to replace the "text" with the "value" in the datumTokenizer. Hope this helps.

+11
source

Source: https://habr.com/ru/post/1202329/


All Articles