I would like some help on Select /Select2 functionality for a "combo" box that I am trying to set up in .Net with knockout/ jquery. I need to preload this "combo" box with an array of values, then allow the user to add additional (select from the dropdown or type in something else) or delete entries. The below code works as it should as long as the values that are being preloaded to the "combo" box exist in the coordinating dropdown (allusers). If the preloaded values do not exist in the dropdown, they are simply bypassed.
Has anyone else had to deal with this issue? How did you overcome it? I would appreciate any feedback or ideas out there!
<select data-bind="selectedOptions: newMessageTo, optionsCaption: 'Select a user...',
options: allUsers, optionsText: 'userName', optionsValue: 'userName',
select2: { placeholder: 'Select a user...', allowClear: true, multiple: true,
minimumInputLength: 3, tags: true, minimumResultsForSearch: 20 }">
</select>
self.myMessage = ko.observable({});
self.myMessageText = ko.observable("");
self.messageId = ko.observable({});
self.newMessageText = ko.observable("");
self.newMessageTo = ko.observableArray([]);
self.newMessageSubject = ko.observable("");
self.newAttachment = ko.observable({});
self.newAttachmentList = ko.observableArray([]);
self.allUsers = ko.observable({});
self.bearer = ko.observable();
self.newStyleAttachments = ko.observable(false);
self.replyAll = function () {
self.replying() ? self.replying(false) : self.replying(true);
self.newMessageTo.removeAll();
for (var i = 0; i < self.myMessage().cc.length; i++) {
if (self.myMessage().cc[i] !== self.user.email) {
self.newMessageTo.push(self.myMessage().cc[i]);
}
}
self.newMessageTo.push(self.myMessage().senderEmail);
self.newMessageText("<br/><center>-------------------------------------------</center><br/> \
<b>From:</b> " + self.myMessage().senderEmail + " <br/><b>To:</b> " + self.myMessage().cc + " \
<br/><b>Subject:</b> " + self.myMessage().messageSubject + "<br/>" + self.myMessageText());
self.newMessageSubject("RE: " + self.myMessage().messageSubject);
};
ko.bindingHandlers.select2 = {
init: function (el, valueAccessor, allBindingsAccessor, viewModel) {
ko.utils.domNodeDisposal.addDisposeCallback(el, function () {
$(el).select2('destroy');
});
var allBindings = allBindingsAccessor(),
select2 = ko.utils.unwrapObservable(allBindings.select2);
$(el).select2(select2);
},
update: function (el, valueAccessor, allBindingsAccessor, viewModel) {
var allBindings = allBindingsAccessor();
if ("value" in allBindings) {
if ((allBindings.select2.multiple || el.multiple) && allBindings.value().constructor != Array) {
$(el).val(allBindings.value().split(',')).change();
}
else {
$(el).val(allBindings.value()).change();
}
} else if ("selectedOptions" in allBindings) {
var converted = [];
var textAccessor = function (value) { return value; };
if ("optionsText" in allBindings) {
textAccessor = function (value) {
var valueAccessor = function (item) { return item; }
if ("optionsValue" in allBindings) {
valueAccessor = function (item) { return item[allBindings.optionsValue]; }
}
var items = $.grep(allBindings.options(), function (e) { return valueAccessor(e) == value });
if (items.length == 0 || items.length > 1) {
return "UNKNOWN";
}
return items[0][allBindings.optionsText];
}
}
$.each(allBindings.selectedOptions(), function (key, value) {
converted.push({ id: value, text: textAccessor(value) });
});
$(el).select2("data", converted);
}
$(el).change();
}
};