jquery - Select2js does not work with multiselect and data array -
i have spent hours trying make work does! able select first item; not 2nd item or more. here code.
$(document).ready(function() { function format(item) { return item.fullname; }
$("#mytest1").select2({ data: { results: [ { memberid: 1, fullname: "user1 last" }, { memberid: 2, fullname: "user2 last" }, { memberid: 3, fullname: "user3 last" }, { memberid: 4, fullname: "user4 last" } ], text: 'fullname' }, id: function (obj) { return '#mytest1'; }, formatselection: format, maximumselectionsize: 3, initselection: function (element, callback) { console.log("init selection", element); var data = []; $(element.val().split(",")).each(function () { data.push({id: this, text: this}); }); callback(data); }, //minimuminputlength: 1, multiple: true, formatresult: format });
});
here fiddlejs link http://jsfiddle.net/cphelefu/nngqgstz/1/
the id function expects retrieve id of selected object, not id of select2. in other words, function should changed from
id: function (obj) { return '#mytest1'; },
to
id: function (obj) { return obj.memberid; },
updated fiddle here: http://jsfiddle.net/nngqgstz/3/
Comments
Post a Comment