admin管理员组文章数量:1391982
I am using typeahead.js 0.11.1 and try to sort the results ing from a remote source. According to the code there should be the possibility to override the default sort function of bloodhound. But my sort function is never called. Same counts for the identify function.
Here is my code:
var bloodhoundSearchEngine = new Bloodhound({
// we do not need any tokenization cause this will be done on the server
datumTokenizer : function(d) {
return d;
},
queryTokenizer : function(q) {
return q;
},
sorter : function(itemA, itemB) {
console.log(itemA);
if (itemA.count < itemB.count) {
return -1;
} else if (itemA.count > itemB.count) {
return 1;
} else
return 0;
},
identify : function(item) {
console.log(itemA);
return item.value;
},
remote : {
url : '/suggest/?term=%term',
wildcard : '%term',
transform : function(response) {
return $.map(response.suggestItems, function(item) {
return {
value : item.value,
count : item.count
};
});
},
rateLimitBy : 'debounce',
rateLimitWait : 300
}
});
$('#typeaheadinput .typeahead')
.typeahead(
{
hint : true,
highlight : true,
minLength : 1
},
{
name : 'existing-names',
display : 'value',
limit : 20,
source : bloodhoundSearchEngine.ttAdapter()
});
Has anyone any hints on how to achieve this?
I am using typeahead.js 0.11.1 and try to sort the results ing from a remote source. According to the code there should be the possibility to override the default sort function of bloodhound. But my sort function is never called. Same counts for the identify function.
Here is my code:
var bloodhoundSearchEngine = new Bloodhound({
// we do not need any tokenization cause this will be done on the server
datumTokenizer : function(d) {
return d;
},
queryTokenizer : function(q) {
return q;
},
sorter : function(itemA, itemB) {
console.log(itemA);
if (itemA.count < itemB.count) {
return -1;
} else if (itemA.count > itemB.count) {
return 1;
} else
return 0;
},
identify : function(item) {
console.log(itemA);
return item.value;
},
remote : {
url : '/suggest/?term=%term',
wildcard : '%term',
transform : function(response) {
return $.map(response.suggestItems, function(item) {
return {
value : item.value,
count : item.count
};
});
},
rateLimitBy : 'debounce',
rateLimitWait : 300
}
});
$('#typeaheadinput .typeahead')
.typeahead(
{
hint : true,
highlight : true,
minLength : 1
},
{
name : 'existing-names',
display : 'value',
limit : 20,
source : bloodhoundSearchEngine.ttAdapter()
});
Has anyone any hints on how to achieve this?
Share Improve this question asked May 5, 2015 at 9:24 René ReitmannRené Reitmann 3642 silver badges17 bronze badges 2- can you please create a demo on jsfiddle ? – Dhiraj Commented May 5, 2015 at 16:24
- Have never tried jsfiddle but I have found a way to solve the problem. See my answer. – René Reitmann Commented May 7, 2015 at 8:33
2 Answers
Reset to default 6The sorter is not invoked cause I use a custom transform function to transform the suggestions ing from the remote server. Therefore I included a call to the sorter in my transform function. The following code works for me:
var bloodhoundSearchEngine = new Bloodhound({
// we do not need any tokenization cause this will be done on the server
datumTokenizer : function(d) {
return d;
},
queryTokenizer : function(q) {
return q;
},
sorter : function(itemA, itemB) {
console.log(itemA);
if (itemA.count < itemB.count) {
return -1;
} else if (itemA.count > itemB.count) {
return 1;
} else
return 0;
},
identify : function(item) {
console.log(itemA);
return item.value;
},
remote : {
url : '/suggest/?term=%term',
wildcard : '%term',
transform : function(response) {
return $.map(bloodhoundSearchEngine.sorter(response.suggestItems), function(item) {
return {
value : item.value,
count : item.count
};
});
},
rateLimitBy : 'debounce',
rateLimitWait : 300
}
});
$('#typeaheadinput .typeahead')
.typeahead(
{
hint : true,
highlight : true,
minLength : 1
},
{
name : 'existing-names',
display : 'value',
limit : 20,
source : bloodhoundSearchEngine.ttAdapter()
});
The sorted
function was not getting called for me for some reason either, and thanks to René I was able to get it working.
var blood = new Bloodhound({
sorter: function(a, b) {
var input_string = $(selector).val();
distance = Levenshtein.get(a.name, input_string) - Levenshtein.get(b.name, input_string)
return distance;
},
remote: {
transform : function(response) {
return blood.sorter(response)
},
},
本文标签: javascriptHow to sort search results in typeaheadbloodhoundStack Overflow
版权声明:本文标题:javascript - How to sort search results in typeaheadbloodhound? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744626404a2616284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论