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)
},
},