javascript - Twitter typeahead not working with with ajax -
i'm quite inexpert frontend world , i'm trying make working.
$('#the-basics .typeahead').typeahead({ hint: true, highlight: true, minlength: 6 }, { source: function (query, process) { return $.get('autocompletecodicefiscale', { codicefiscale: query }, function (data) { //alert(json.stringify(data)); return process(data.options); }); } }).on('typeahead:selected', function (evt, data) { //mocking data if (data.length >= 16) { $('#nome').val("mohamed"); $('#cognome').val("shafik"); $('#sesso').val("maschio"); $('#codiceregionale').val("romab"); $('#codicefiscale').val("shfmmd81d06z336b"); $('#datanascita').val("6/04/1981"); $('#indirizzo').val("via della prova"); $('#numerocivico').val("123"); $('#comune').val("roma"); $('#cap').val("00100"); $('#telfisso').val("0612345678"); $('#telmobile').val("3471234567"); $('#email').val("prova@lait.it"); $('#aslcircoscrizione').val("roma iv"); } else { // other stuff } }); this give no errors not show returned data, alert shows informations requested returned correctly.
if use this
var substringmatcher = function(strs) { return function findmatches(q, cb) { var matches, substringregex; // array populated substring matches matches = []; // regex used determine if string contains substring `q` substrregex = new regexp(q, 'i'); // iterate through pool of strings , string // contains substring `q`, add `matches` array $.each(strs, function(i, str) { if (substrregex.test(str)) { matches.push(str); } }); cb(matches); }; }; var anagrafica = ['shfmmd81d06z336b - mohamed shafik - 06/04/1981', 'shfmmd81d06z336b - mohamed shafik - 06/04/1982', 'mrtclg81d08g442b - calogero martello - 01/01/1981' ]; $('#the-basics .typeahead').typeahead({ hint: true, highlight: true, minlength: 6 }, { name: 'anagrafica', source: substringmatcher(anagrafica) }).on('typeahead:selected', function(evt, data) { //mock if (data.length >= 16) { $('#nome').val("mohamed"); $('#cognome').val("shafik"); $('#sesso').val("maschio"); $('#codiceregionale').val("romab"); $('#codicefiscale').val("shfmmd81d06z336b"); $('#datanascita').val("6/04/1981"); $('#indirizzo').val("via della prova"); $('#numerocivico').val("123"); $('#comune').val("roma"); $('#cap').val("00100"); $('#telfisso').val("0612345678"); $('#telmobile').val("3471234567"); $('#email').val("prova@lait.it"); $('#aslcircoscrizione').val("roma iv"); } else { //do other stuff } }); everything works fine.
my controller looks this
@requestmapping(value = { "autocompletecodicefiscale" }, method = requestmethod.get, produces="application/json") public @responsebody tempclass autocompletecodicefiscale(@requestparam string codicefiscale, modelmap map) { list<assistitodto> assistiti = //requested data db tempclass autocomplete = new tempclass(); for(assistitodto assistito : assistiti) { autocomplete.options = new arraylist<string>(); autocomplete.options.add(assistito.getanagrafica().getcodicefiscale() + " - " + assistito.getanagrafica().getcognome() + " " + assistito.getanagrafica().getnome() + " - " + assistito.getanagrafica().getdatanascita().getday() + "/" + assistito.getanagrafica().getdatanascita().getmonth() + "/" + assistito.getanagrafica().getdatanascita().getyear()); } system.out.println("la string json è: " + new gson().tojson(autocomplete)); return autocomplete; } private class tempclass { public list<string> options; } it looks ugly know i'm testing why it's not working @ moment. don't think need show css because works correctly mock. way suggestions aren't getting populated, it's not visual issue. apologize english.
oh god solved it, i've chenged quite bit, , thats result,
var people = new bloodhound({ datumtokenizer: bloodhound.tokenizers.obj.whitespace('codicefiscale'), querytokenizer: bloodhound.tokenizers.whitespace, remote: { url: 'autocompletecodicefiscale/?codicefiscale=%query', wildcard: '%query' } }); people.initialize(); $('#the-basics .typeahead').typeahead({ minlength: 6 }, { limit: 10, name: 'people', display: 'codicefiscale', source: people.ttadapter(), templates: {empty: [ '<div class="empty-message">', 'nessun assistito / assistibile trovato', '</div>' ].join('\n'), suggestion: handlebars.compile('<div><strong>{{codicefiscale}}</strong> - {{cognome}} {{nome}} - {{datanascita}}</div>') } }).on('typeahead:selected', function(obj, datum) { // stuff }); </script> and controller has been modified too
@requestmapping(value = { "autocompletecodicefiscale" }, method = requestmethod.get, produces="application/json") public @responsebody list<temp> autocompletecodicefiscale(@requestparam string codicefiscale, modelmap map) { list<assistitodto> assistiti = //data db list<temp> autocomplete = new arraylist<temp>(); for(assistitodto assistito : assistiti) { temp asstemp = new temp(); asstemp.codicefiscale = assistito.getanagrafica().getcodicefiscale(); asstemp.nome = assistito.getanagrafica().getnome(); asstemp.cognome = assistito.getanagrafica().getcognome(); asstemp.datanascita = assistito.getanagrafica().getdatanascita().getday() + "/" + assistito.getanagrafica().getdatanascita().getmonth() + "/" + assistito.getanagrafica().getdatanascita().getyear(); autocomplete.add(asstemp); } system.out.println("la string json è: " + new gson().tojson(autocomplete)); return autocomplete; } private class temp { public string codicefiscale; public string nome; public string cognome; public string datanascita; } and works this.
Comments
Post a Comment