Just send the typed term to the server and execute a sql-statement similar to this one:
select name from tag t left outer join tagSyms s on s.tagID=t.tagID
where s.alias=$searchstring OR t.tagName=$searchstring
and return json formatted data. JSON-Format: { val: 'tagname' }
On Client site:
var engine = new Bloodhound({
name: 'tags',
remote: 'http://example.com/tags?q=%QUERY',
datumTokenizer: function(d) {
return Bloodhound.tokenizers.whitespace(d.val);
},
queryTokenizer: Bloodhound.tokenizers.whitespace
});
engine.initialize();
$("#inputField").typeahead(...);
Also take a look at the displayKey and template keys in the typeahead.js
When you want a sorted list, you will be fine if the sql is ordered.
You control what in the suggestion box gets displayed with the templates:
For example if you want to display the counter too, just edit the JSON to { val: 'tagname', counter:x }
and the typeahead to:
.typeahead(
....,{
templates:{
suggestion:function(data){
return "<p>"+data.val+"(used: "+data.counter+" times)</p>";
}
},
source: engine.ttAdapter()
});