2

I'm using EasyAutocomplete to select some data.

The problem I'm having is that I only can use one autocomplete input. I tried to find a solution but I didn't find anything in the documentation.

My code:

<%= form_tag('/search', local: true, method: :get, class: 'form-group row' ) do %>
    <div class="col-md-5">
        <%= text_field_tag(:q, nil, data: { behavior: 'autocomplete-lang' }, placeholder: 'Lenguaje', class: 'form-control') %>
    </div>
<% end %>
<%= form_tag('/search', local: true, method: :get, class: 'form-group row' ) do %>
    <div class="col-md-5">
        <%= text_field_tag(:q, nil, data: { behavior: 'autocomplete-ide' }, placeholder: 'Lenguaje', class: 'form-control') %>
    </div>
<% end %>

Controller:

def search
    @lang = Content.ransack(name_cont: params[:q],types_id_eq: 1).result(distinct: true)
    @ide = Content.ransack(name_cont: params[:q],types_id_eq: 4).result(distinct: true)
    respond_to do |format|
      format.html {}
      format.json {
        @lang = @lang.limit(5)
        @ide = @ide.limit(5)
      }
    end
end

Js:

document.addEventListener("turbolinks:load", function() {
    var $input = $("[data-behavior='autocomplete-lang']");


    var options = {
        getValue: "name",
        url : function(phrase) {
            return "/search.json?q=" + phrase;
        },
        categories: [
            {
                listLocation: "lang"
            }
        ],
        list: {
            onChooseEvent: function () {
                var id = $input.getSelectedItemData().id;
                console.log(id);
            }
        }
    };

    $input.easyAutocomplete(options);

});

document.addEventListener("turbolinks:load", function() {
    var $input_ide = $("[data-behavior='autocomplete-ide']");
    //IDE
    var options_ide = {
        getValue: "name",
        url : function(phrase) {
            return "/search.json?q=" + phrase;
        },
        categories: [
            {
                listLocation: "ide"
            }
        ],
        list: {
            onChooseEvent: function () {
                var id = $input_ide.getSelectedItemData().id;
                console.log(id);
            }
        }
    };

    $input_ide.easyAutocomplete(options_ide);
});

So, I only can use once the easyAutocomplete, if I use it twice the behavior is not correct and only one input work.

I really need a hand to solve this, is must be pretty simple, but I can't get it.

4
  • You could elaborate on how "the behavior is not correct" when you try to use two. Commented Jun 27, 2017 at 7:27
  • Only one input field works Commented Jun 27, 2017 at 7:30
  • So what you're saying is that only one of the inputs shows the autocomplete dropdown? Which one? Commented Jun 27, 2017 at 7:31
  • @vijoc autocomplete-lang. And console.log shows undefined but if I comment $input_ide.easyAutocomplete(options_ide); shows the correct id Commented Jun 27, 2017 at 7:39

2 Answers 2

3

I faced the same problem and solved it by adding unique "id"s to each text field.

Sign up to request clarification or add additional context in comments.

Comments

3

In JavaScript, instead of writing:

getValue: "name"

You can write something similar to:

getValue: function (element) {
    return element.lang+ element.ide
}

You can follow the discussion on this github link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.