javascript - Select2 4.0 clicking item doesn't fire select2:select -
i'm using select2 4.0 , when load data json file none of created elements fire select2:select
event. thought have event delegation, examples on website seem fine.
i've been breaking head on past hour , don't understand i'm missing.
right i'm using select item, doesn't work:
$("body").on("click", "#select2-select-results .person-entry", function() { var $this = $(this); var name = $this.data('name'); select2.find('option').val(name).text(name); });
binding $(...).on('select2:select');
never fires, $(...).on('select2:open);
does. i'm not sure what's going on.
what did notice however, when data has been loaded, no <option>
tags created.
this entire javascript that's handling thing:
(function($) { "use strict"; var term = null; var select2 = $("#select"); select2.select2({ allowclear: true, ajax: { url: "js/data.json", datatype: "json", delay: 250, data: function(params) { // save terms filtering term = params.term; return params; }, processresults: function(data) { var people = []; for(var = 0; < data.people.length; i++) { var person = data.people[i]; var filter = term || ''; if (person.name.tolowercase().indexof(filter.tolowercase()) !== -1) { people.push(person); } } return { results: people }; } }, templateresult: function(person) { if (person.loading) { return person.text; } var persondetails = '<div class="person-information">' + '<span class="person-name">' + person.name + '</span>' + '<span class="person-address">' + person.address + '</span>' + '</div>'; return '<div class="person-entry" data-name="' + person.name + '"><img src="' + person.image + '" />' + persondetails + '</div>'; }, templateselection: function(person) { return person.name; }, escapemarkup: function(markup) { return markup } }); $("body").on("click", "#select2-select-results .person-entry", function() { var $this = $(this); var name = $this.data('name'); select2.find('option').val(name).text(name); }); })(jquery);
can tell me i'm doing wrong here? don't it.
it sounds not setting id
field on data objects. without field, select2 cannot know set value
of <select>
to, doesn't allow selected.
additionally, text
field required searching , (by default) displaying data in results list.
Comments
Post a Comment