How do I display results with autocomplete.js?

I am using autocomplete.js for the results. i did searching through a input field value. Now i want to search name through input field value and a fix city name. I used facetFilters but it’s not working.
I have Json file with name and city parameter.

here is my sample code.

autocomplete('#search_text', {}, [
    {
      source: autocomplete.sources.hits(practice_area, { hitsPerPage: 4}),
      displayKey: 'name',
      templates: {
        suggestion: function(suggestion) {
          return '<span>' +
            suggestion._highlightResult.name.value + '</span><span style="float:right;">Practice Area</span>';
        }
      }
    },
    {
      source: autocomplete.sources.hits(lawyers, { hitsPerPage: 4, facetFilters: 'city:San Diego' }),
      displayKey: 'name',
      templates: {
        suggestion: function(suggestion) {
          return '<span>' +
            suggestion._highlightResult.name.value + '</span><span style="float:right;">Lawyers</span>';
        }
      }                          
    }   
]).on('autocomplete:selected', function(event, suggestion, dataset) {
    console.log(suggestion, dataset);
});

Thank you.

If you can create a codepen or jsfiddle to illustrate your problem that would be very helpful.

1 Like

https://jsfiddle.net/hksunny10/ksj1ftga/6/

Hi,

Thank you for the link.
If I understand correctly, you want to look for practice’s names given a fix city name (in this case, San Diego).
It seems that it is working fine: https://jsfiddle.net/8j0xppyg/ (we have outputted the city and it only shows hits from San Diego).

Does this help?

Let us know if you have any questions!

Best,

Not sure what you are trying to achieve exactly, but your facet filter is working: https://jsfiddle.net/ksj1ftga/8/
You are only getting results for San Diego, which is what the facetFilter you set has done.

Heh, beat me to it :wink: