javascript - Twitter typeahead default suggestions: how to get the first suggestions in the prefetched array? -
i using twitter typeahead , offer values user when input box gets focus, before typed.
there's example on examples page under default suggestions:
var nflteams = new bloodhound({ datumtokenizer: bloodhound.tokenizers.obj.whitespace('team'), querytokenizer: bloodhound.tokenizers.whitespace, identify: function(obj) { return obj.team; }, prefetch: '../data/nfl.json' }); function nflteamswithdefaults(q, sync) { if (q === '') { sync(nflteams.get('detroit lions', 'green bay packers', 'chicago bears')); } else { nflteams.search(q, sync); } } $('#default-suggestions .typeahead').typeahead({ minlength: 0, highlight: true }, { name: 'nfl-teams', display: 'team', source: nflteamswithdefaults });
however in example, hard-code values want suggest:
sync(nflteams.get('detroit lions', 'green bay packers', 'chicago bears'));
in case, use bloodhound pre-fetch, , want suggest first few options returned pre-fetch.
edit: show first few items returned server. server responsible order of showing them. in above example, if data/nfl.json contains:
[ { "team": "san francisco 49ers" }, { "team": "chicago bears" }, { "team": "cincinnati bengals" }, { "team": "buffalo bills" }, { "team": "denver broncos" }, { "team": "cleveland browns" }, ...
i show whatever first teams are. number of teams shown parameter.
how do that?
for example, want display 2 first items in default
function nflteamswithdefaults(q, sync) { if (q === '') { sync(nflteams.all().slice(0, 2)); // slice(start,end) } else { nflteams.search(q, sync); } }
Comments
Post a Comment