我试图在比赛中使用把手显示每个主题的结果数量,如下所示:
这是我到目前为止的代码:
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>')
}
});
我一直在尝试不同的方法,而我似乎无法弄明白。
修改
没有工作结果的jsFiddle http://jsfiddle.net/0n0b2ue4/2/
jsFiddle with data http://jsfiddle.net/0n0b2ue4/3/
答案 0 :(得分:2)
根据typeahead docs,上下文传递到header
模板:
将包含
query
和suggestions
Suggestions
包含该数据集的当前建议列表,并公开了一个length属性。因此,只需在标题的句柄模板中使用{{suggestions.length}}
(不是{{count}}
)。
// constructs the suggestion engine
var clients = new Bloodhound({
local: ["A Client", "AA Client", "BC Client"],
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace
});
var contacts = new Bloodhound({
local: ["BA Contact", "BB Contact", "CC Contact"],
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace
});
var tasks = new Bloodhound({
local: ["A Client", "AA Client", "BC Client"],
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace
});
// initialize typeahead by passing in options and data
$("#clients").typeahead({
hint: true,
minLength: 1
}, {
name: 'clients',
source: clients,
templates: {
header: Handlebars.compile($("#clients-header").html())
}
}, {
name: 'contacts',
source: contacts,
templates: {
header: Handlebars.compile($("#contacts-header").html())
}
}, {
name: 'tasks',
source: tasks,
templates: {
header: Handlebars.compile($("#tasks-header").html())
}
});
.search_header {
margin-left: 6px;
}
div.tt-menu {
margin-top: 0px;
}
<link href="http://twitter.github.io/typeahead.js/css/examples.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.3/handlebars.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.js"></script>
<input type="text" id="clients" class="typeahead" />
<!-- Templates -->
<script id="clients-header" type="text/x-handlebars-template">
<strong class="search_header">
Clients <small>({{suggestions.length}}) results</small>
</strong>
</script>
<script id="contacts-header" type="text/x-handlebars-template">
<strong class="search_header">
Contacts <small>({{suggestions.length}}) results</small>
</strong>
</script>
<script id="tasks-header" type="text/x-handlebars-template">
<strong class="search_header">
Tasks <small>({{suggestions.length}}) results</small>
</strong>
</script>