Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knockout optionsText is encoded

I am trying to create an html hierarchical select based on this solution and using knockout

However, knockout encodes the string value i return.

How can i decode the text returned from the function?

jsFiddle example

Html:

<select data-bind="options: items, optionsText: getOptionText"></select>

Javascript:

var viewModel = {
    items: ko.observableArray([
        { Text: "Item 1", level: 1 },
        { Text: "Item 2", level: 2 },
        { Text: "Item 3", level: 3 },
        { Text: "Item 4", level: 4 }
    ]),
    getOptionText: function(data) {
        var value = "";
        for (var i = 1; i <= (data.level - 1) * 2; i++) {
            value += "&nbsp;";
        }
        value += data.Text;
        return value;
    }
};
ko.applyBindings(viewModel)
like image 647
Catalin Avatar asked Jan 21 '14 12:01

Catalin


2 Answers

Replace your "&nbsp;" with "\xA0" (The hexadecimal representation of the Non-Breaking Space character.)

like image 192
php-dev Avatar answered Nov 18 '22 06:11

php-dev


The answer is to use the actual character for a space instead of an HTML entity. To get a non-breaking space type Alt+255. Just do this in place of the &nbsp; in your code. It will look like a normal space but will display as a non-breaking space:

Live example: http://jsfiddle.net/jLbct/1/

like image 31
Jamiec Avatar answered Nov 18 '22 08:11

Jamiec