I am currently having an odd problem with my optionsText within a multi-select list I am building with Knockout.
I noticed that some of the text data that I am retrieving from my controller is HTML encoded and I would like to have it displayed properly within my select list.
Simple Example
String from my controller (and how it's currently displaying)
Dave & Buster
How I would like it displayed
Dave & Buster
I tried to use a function in the optionsText to perform the decoding without creating a computed, but for some reason, the decode didn't work at all.
html-data-bind="options: ViewModel.AvailableItems,
optionsText: function(item) { return decodeURI(item.Label()) },
optionsValue: 'Value',
selectedOptions: ViewModel.ItemsSelected"
I am currently populating my view model using the the Knockout Mapping plug-in, so that is a big reason I didn't use computeds to resolve this. This way I don't have to mess with mapping settings any time the model gets additional properties.
I tried using the optionsAfterRender
binding with the following code (Fiddle I found it from and the question I found it from)
ViewModel.setOptionHTML = function(option, html) {
ko.applyBindingsToNode(option, {
html: html
})
}
However, when the optionText
is rendered, it shows up as [object][object].
What am I missing in order to get my text to display properly in my select list?
Here is an example to show the issue:
var vm = {
ViewModel: {
AvailableItems: [
{
Label: ko.observable("Dave & Buster"),
Value: 1
}
],
ItemSelected: ko.observable()
}
};
ko.applyBindings(vm);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options: ViewModel.AvailableItems,
optionsText: function(item) { return decodeURI(item.Label()) },
optionsValue: 'Value',
selectedOptions: ViewModel.ItemsSelected">
</select>