Searchkick - Autocomplete with multiple attributes

Autocomplete works fine when searching with a single attribute, as indicated here .

Autocomplete with several attributes, such as (name, city, country), possibly through → (in accordance with this )

def autocomplete
     Doctor.search(params[:query], autocomplete: true, limit: 10).map{|doctor| doctor.slice(:name, :city, :country) }
end

However, this leads to a drop-down list / auto-complete suggestions to show "undefined".

For type forward, I use:

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"></script>

In the code it is referenced:

$( function () {
   $("#search").typeahead({
    name: "doctor",
    remote: "/doctors/autocomplete?query=%QUERY"
  });


});

Is there any change to the typeahead file's java file due to more than one set of returned data?

+1
source share
2

hash

autocomplete doctors :

def autocomplete
  render json: Doctor.search(params[:query], autocomplete: true, limit: 10).map do |doctor| { name: doctor.name, city: doctor.city, country: doctor.country }
  end
end

displayKey typeahead:

$( function () {
   $("#search").typeahead({
    name: "doctor",
    displayKey: 'name',
    remote: "/doctors/autocomplete?query=%QUERY"
  });
});

, .

+2

this this
, , :

def autocomplete
    names = Doctor.search(params[:query], fields: [{name: :text_start}], limit: 10).map {|Doctor| {store: doctor.name, value: doctor.id}}
    collegenames = Doctor.search(params[:query], fields: [{collegename: :text_start}], limit: 10).map {|Doctor| {store: doctor.collegename, value: doctor.id}}
    render json: (names + collegenames)

end

: .
Javascript:

var ready;
ready = function() {
    console.log("dfdf")
    var numbers = new Bloodhound({
      datumTokenizer: function(d) {
            console.log(d);
            return Bloodhound.tokenizers.whitespace('value');
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: {
            url:"/doctors/autocomplete?query=%QUERY"
        }


        });

        // initialize the bloodhound suggestion engine

        var promise = numbers.initialize();

        promise
        .done(function() { console.log('success!'); })
        .fail(function() { console.log('err!'); });

        // instantiate the typeahead UI
        $('.typeahead').typeahead(null, {
          displayKey: 'store',
          source: numbers.ttAdapter()
        });
}

$(document).ready(ready);
$(document).on('page:load', ready);

Autocomplete , URL-,

http://localhost:3000/doctors/autocomplete?query="a"
+1

All Articles