0

having JS like:

$(".select2-input").select2({
        placeholder: placeholder,
        id: function(data){return data.id},
        ajax: {
            url: "/autocomplete",
            datatype: 'json',
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            results: function (data) {
                return {
                    results: data.results
                };
            },
            cache: true,
            id: function(connection){
                console.log(connection);
            }
        }
    });

and results from "/autocomplete" URL like this:

{"results":[{"id":14953,"text":"Dohn Doe"},{"id":15467,"text":"Jane Dohe"}]}

With all of this I can see autocomplete results, but clicking on any results causes no changes!

So I've been trying to implement answer from this question, but with no luck. Probably I just missed/messed something.

Also this line

console.log(connection);

writes nothing to console.

Select2 version is 4.0

Community
  • 1
  • 1
sheepwalker
  • 1,690
  • 2
  • 13
  • 17
  • You shouldn't need to implement `id` since you have an id. Where do you define `placeholder`? Are there any errors in the JS console? – Dave Newton Sep 21 '15 at 14:19
  • ```placeholder``` is defined above, let's say as ```placeholder = 'abc';```. And I don't have any JS errors here :( – sheepwalker Sep 22 '15 at 12:49
  • If nothing else, `datatype` should be `dataType` (capital T). You shouldn't need `results` if your results are already called `results`. – Dave Newton Sep 22 '15 at 13:23
  • Thanks Dave for your help, but it's still on the same point even with: `dataType` and `return data.results;` – sheepwalker Sep 23 '15 at 08:27
  • What specifically do you mean by "clicking on any results causes no changes"? – Dave Newton Sep 23 '15 at 11:41

1 Answers1

0

Ok, seems like I found the problem. First of all need to say, that select2 from version 4.0 does not support id option I've concerned above. This is the extract from documentation to prove it:

Select2 no longer supports a custom id or text to be used, but provides integration points for converting incorrect data to the expected format.

So the problem was not in the code I've provided (except dataType type, thanks Dave Newton!), but in the code I didn't provide. So there I had

input :person, as: :text, input_html: { class: "select2-input", "data-placeholder" => person }

– notice the as: :text part

and once I've changed it to

input :person, as: :select, input_html: { class: "select2-input", "data-placeholder" => person }

everything worked like a charm! Still don't know what drives me to make it text typed instead of select.

sheepwalker
  • 1,690
  • 2
  • 13
  • 17