Algolia 自动完成 - 如何从自动完成建议中删除 "administrative" municipalities/districts
Algolia autocomplete - how to remove "administrative" municipalities/districts from autocomplete suggestions
我正在集成 Algolia 自动完成功能,不喜欢自动完成建议的外观。
具体来说,我不希望建议中出现行政直辖市和区,只有address, city, country。
如何省略管理查询?
例如,如果我输入 "Sarajevo",建议显示为 "Sarajevo, Kanton of Sarajevo, Bosnia and Herzegovina" - 我希望它显示为 "Sarajevo, Bosnia and Herzegovina".
您应该使用 Places 构造函数的 templates
选项。
这是一个简单的例子:
const placesInstance = places({
...
templates: {
suggestion: function(s) {
return [s.highlight.name, s.highlight.city, s.highlight.country].join(', ');
}
}
});
查看默认使用的函数以获得更详细的示例:
https://github.com/algolia/places/blob/master/src/formatDropdownValue.js
要解决一旦你select一个'place',管理级别就会显示在搜索栏中。,你可以利用jquery的聚焦事件。
示例
var cityCountry ,searchInput;
searchInput = $('#search-input'); //our search field
//Initialize
var placesAutocomplete = places({
// appId: 'YOUR_PLACES_APP_ID',
// apiKey: 'YOUR_PLACES_API_KEY',
container: document.querySelector('#search-input'),
});
//Attach required data to cityCountry
placesAutocomplete.on('change', function(e){
let suggestion,city,country;
suggestion = e.suggestion;
city = suggestion.name;
country= suggestion.country;
cityCountry = city+', '+country;
});
//Manipulate the search field on focusout
searchInput.on('focusout', function(){
setTimeout(function () {
searchInput.val(cityCountry);
},1)
});
请注意,如果没有 setTimeout()
,它将无法工作。
我正在集成 Algolia 自动完成功能,不喜欢自动完成建议的外观。 具体来说,我不希望建议中出现行政直辖市和区,只有address, city, country。 如何省略管理查询?
例如,如果我输入 "Sarajevo",建议显示为 "Sarajevo, Kanton of Sarajevo, Bosnia and Herzegovina" - 我希望它显示为 "Sarajevo, Bosnia and Herzegovina".
您应该使用 Places 构造函数的 templates
选项。
这是一个简单的例子:
const placesInstance = places({
...
templates: {
suggestion: function(s) {
return [s.highlight.name, s.highlight.city, s.highlight.country].join(', ');
}
}
});
查看默认使用的函数以获得更详细的示例: https://github.com/algolia/places/blob/master/src/formatDropdownValue.js
要解决一旦你select一个'place',管理级别就会显示在搜索栏中。,你可以利用jquery的聚焦事件。
示例
var cityCountry ,searchInput;
searchInput = $('#search-input'); //our search field
//Initialize
var placesAutocomplete = places({
// appId: 'YOUR_PLACES_APP_ID',
// apiKey: 'YOUR_PLACES_API_KEY',
container: document.querySelector('#search-input'),
});
//Attach required data to cityCountry
placesAutocomplete.on('change', function(e){
let suggestion,city,country;
suggestion = e.suggestion;
city = suggestion.name;
country= suggestion.country;
cityCountry = city+', '+country;
});
//Manipulate the search field on focusout
searchInput.on('focusout', function(){
setTimeout(function () {
searchInput.val(cityCountry);
},1)
});
请注意,如果没有 setTimeout()
,它将无法工作。