要突出显示搜索自动完成的项目,可以使用Algolia的configure函数和highlightTags属性。以下是一个示例代码片段:
search.addWidget(
instantsearch.widgets.autocomplete({
container: "#autocomplete-input",
hitsPerPage: 5,
templates: {
suggestion: function(hit) {
return '' + hit._highlightResult.title.value + '';
}
},
configure: {
highlightTags: {
preTag: '',
postTag: ''
}
}
})
);
在上面的示例中,highlightTags属性被设置为preTag:和postTag:。这导致匹配的搜索结果以粗体字来显示。
配置函数configure将此属性应用于搜索自动完成小部件。
在模板函数中,使用_highlightResult对象来获得突出显示的结果,如上例中模板的字符串所示。