I'm trying to show the amount of results showing per subject using handlebars in typeahead, like this:
Here's the code I have so far:
<!-- language: lang-js -->var clients = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('client_name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'clients.php?query=%QUERY',
wildcard: '%QUERY'
}
});
var contacts = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('contact_firstname'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'contacts.php?query=%QUERY',
wildcard: '%QUERY'
}
});
var tasks = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('assignment_subject'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'tasks.php?query=%QUERY',
wildcard: '%QUERY'
}
});
clients.initialize();
contacts.initialize();
tasks.initialize();
$("#clients").typeahead({
hint: true,
// highlight: true,
minLength: 1
},{
name: 'clients',
displayKey: 'client_name',
source: clients.ttAdapter(),
templates: {
header: Handlebars.compile('<div class="search_header">Kundkort <span>(<span class="test">{{#each client_name}} {{counter @index}} {{@index}} {{/each}}</span>) träffar</span></div>'),
suggestion: Handlebars.compile('<div><span class="act_link"><a href="?p=crm&client_id={{client_id}}&action=view">{{client_name}}</a></span> <div class="client_type">{{#ifCond client_type "==" 1}} A-kund {{else}} {{#ifCond client_type "==" 2}} Återförsäljare {{/ifCond}} {{#ifCond client_type "==" 3}} Leverantör {{/ifCond}} {{#ifCond client_type "==" 4}} Partner {{/ifCond}} {{#ifCond client_type "==" 5}} Prospekt {{/ifCond}} {{/ifCond}}</div></div>')
}
},{
name: 'contacts',
displayKey: 'contact_firstname',
source: contacts.ttAdapter(),
templates: {
header: Handlebars.compile('<div class="search_header">Personer <span>(<span class="test">{{c_count}}</span>) träffar</span></div>'),
suggestion: Handlebars.compile('<div><span class="act_link"><a href="?p=crm&client_id={{client_id}}&action=view">{{contact_firstname}} {{contact_lastname}}</a></span> <div class="client_type">{{client_name}}</div></div>')
}
},{
name: 'tasks',
displayKey: 'assignment_subject',
source: tasks.ttAdapter(),
templates: {
header: Handlebars.compile('<div class="search_header">Uppgifter <span>(<span class="test">{{tasks.id.length}}</span>) träffar</span></div>'),
suggestion: Handlebars.compile('<div><span class="act_link"><a href="?p=tasks&id={{id}}&action=view">{{assignment_subject}}</a></span></div>')
}
});
I've been trying different methods and I can't seem to figure it out.
EDIT
jsFiddle without working results http://jsfiddle.net/0n0b2ue4/2/
jsFiddle with data http://jsfiddle.net/0n0b2ue4/3/