2015-05-21 12 views
6

My Typeahead.js/Bloodhound (0.11.1) non funziona come previsto. Dalla lunga lista di risultati json forniti, solo alcuni sono visualizzati come suggerimenti.Typeahead.js/Bloodhound visualizza solo un risultato

Ad esempio, se digito los nel mio campo, ottengo solo Lostorf e nient'altro, quando devono essere visualizzati 4 elementi selezionabili.

Questo è il mio codice:

HTML

<div id="remote"> 
<input class="typeahead" type="text"> 
</div> 

JS

var searchablePlaces = new Bloodhound({ 
    datumTokenizer  : Bloodhound.tokenizers.obj.whitespace("term"), 
    queryTokenizer  : Bloodhound.tokenizers.whitespace, 
    remote    : { 
     url    : 'http://www.example.com/autocomplete/%QUERY/', 
     wildcard  : '%QUERY', 
     filter   : function(response) { return response.data.results; } 
     }, 
    limit    : 10 
}); 

searchablePlaces.initialize(); 

$('#remote .typeahead').typeahead(
{ 
    hint   : true, 
    highlight  : true, 
    minLength  : 2 
}, 
{ 
    name   : 'searchable-places', 
    displayKey  : "term", 
    source   : searchablePlaces.ttAdapter() 
}) 

JSON

{ 
    "data": { 
     "query": "los", 
     "count": 4, 
     "results": { 
      "1": { 
       "term": "Losanna" 
      }, 
      "2": { 
       "term": "Losone" 
      }, 
      "3": { 
       "term": "Lostallo" 
      }, 
      "4": { 
       "term": "Lostorf" 
      } 
     } 
    } 
} 

Vedete qualcosa di sbagliato? Grazie!

+0

Possibile causa: https://github.com/twitter/typeahead.js/issues/1218 –

risposta

12

Questo per confermare che il problema è causato da questo typehaead bug: https://github.com/twitter/typeahead.js/issues/1218

Come suggerito dal joekur nella relazione problema, ho risolto sostituendo questo:

rendered += suggestions.length; 
that._append(query, suggestions.slice(0, that.limit - rendered)); 

Con questo:

suggestions = suggestions.slice(0, that.limit - rendered); 
rendered += suggestions.length; 
that._append(query, suggestions); 

Ho contrassegnato la mia domanda come duplicato di questo: TypeAhead.js and Bloodhound showing an odd number of results È lo stesso problema, non riesco a trovarlo prima a causa di diversi wordi ng

HTH.

+2

In realtà non ho dovuto sostituire il codice, aggiungere solo 'limite: 10' a typeahead ei miei risultati hanno iniziato a mostrare. – kross

+0

Grazie! Ha risolto il problema. Questo mi faceva impazzire per circa 2 ore! –

+0

È ancora il problema, quindi il codice ha davvero aiutato. stava per impazzire :) – Jay

Problemi correlati