javascript - Typeahead remote returns two undefined when there is one remote result left -


i building search information remote json api. cannot pre-fetch data data set large (3000+) , in order results pre-fetch need provide single letter query.

the problem running in when down 1 result left in search, typehead stops giving me results , shows 2 undefined items result.

this looks like:

screenshot

i've tried can think of through console @ point try , debug cannot find anywhere see data coming from.

here code bloodhound , typeahead initialization.

var items = new bloodhound({     datumtokenizer: function (datum) {         return bloodhound.tokenizers.whitespace(datum.k);     },     querytokenizer: bloodhound.tokenizers.whitespace,     remote: {         url: app.target + "crm/sf/list/item/%query/5",         filter: function (parsedresponse) {             return parsedresponse.listing.list.entries;         },     } });  items.initialize();  $('#itemnosrch').typeahead(null, {     autoselect: true,     displaykey: 'k',     source: items.ttadapter() }).on('typeahead:selected', function (obj, datum, name) {     app.searchdata.itemno = datum.k.replace("/", "*");      $('#itemsrchplaceholder').val(datum.k);     app.iteminvalid = false; }); 

edit:

here json response crm/sf/list/item/%query/5 query a.

{ "listing": {         "list": {             "entries": [                 {                     "k": "a-10-10",                     "v": 1320                 },                 {                     "k": "a-10-7",                     "v": 4841                 },                 {                     "k": "a-10-8",                     "v": 4821                 },                 {                     "k": "a14yv4835",                         "v": 1327                 },                 {                     "k": "a0554835",                     "v": 1325                 }             ]         }     } } 

and here how can assume output appearing when occurring, used console on google chrome's latest stable version.

enter image description here

i provide fiddle based on api server not add cross-origin headers @ moment.

please let me know of other information required.

i found problem! remote data set returning object instead of array when 1 result.

the output looked this.

{ "listing": {     "list": {         "entries": {             "k": "a-10-10",             "v": 1320         }     } } 

so added following in filter

filter: function (parsedresponse) {         if(parsedresponse.listing.list.entries instanceof array){             return parsedresponse.listing.list.entries;         }else{             return [parsedresponse.listing.list.entries];         }     }, 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -