Anubis
Anubis

Reputation: 2624

Selectbox with optgroup with knockoutJS

I need to display selectbox of items goup by their categories like this: enter image description here

My C# model is:

public class SandboxModel
{
    public SandboxModel()
    {
        PubCodes = new List<SelectListItem>
                       {
       new SelectListItem { Value = 0, Text = "Agora", Type = 1 }, 
       new SelectListItem { Value = 96, Text = "AGF - Agora Financial", Type = 2 }, 
       new SelectListItem { Value = 81, Text = "CSP - Common Sense Publishing", Type = 2 }, 
       new SelectListItem { Value = 136, Text = "HSI - Health Sciences Institute", Type = 2 },
       new SelectListItem { Value = 0, Text = "Non-Agora", Type = 1 },
       new SelectListItem { Value = 135, Text = "ANG - Angel Publishing", Type = 2 }, 
       new SelectListItem { Value = 123, Text = "APF - Apocalypse Freedom", Type = 2 }, 
       new SelectListItem { Value = 93, Text = "ASI - Asset Strategies International", Type = 2 },
                       };

    }

    public IList<SelectListItem> PubCodes { get; private set; }

    public SelectListItem PubCode
    {
        get { return PubCodes.Last(); }
    }
}

my Javascript model is:

<script type="text/javascript">
    @{ var serializer = new JavaScriptSerializer(); }
    function ViewModel () {
        var self = this;

        // Server Model Properties
        self.SelectedPubCode = ko.observable(@Model.PubCode.Value);

        // Select lists
        self.PubCodes = ko.observableArray(@Html.Raw(serializer.Serialize(Model.PubCodes)));
        };
    };
    ko.applyBindings(new ViewModel());
</script>

my Html is

<select data-bind="foreach:PubCodes, optionsCaption: 'Please Select', value:SelectedPubCode">
    <!-- ko if: Type === 1 -->
    <optgroup label="__________"></optgroup>
    <optgroup data-bind="attr: {label: Text}"></optgroup>
    <!-- /ko -->
    <!-- ko if: Type !== 1 -->
    <option data-bind="text: Text, value: Value"></option>
    <!-- /ko -->
</select>

In all browsers wxcept IE I reach what I needed but in IE it appears like this: enter image description here Any suggestions how to fix that in IE?

Upvotes: 1

Views: 714

Answers (1)

k0lpak
k0lpak

Reputation: 553

You need to use template binding, because IE cuts out comments from <select>.

Template definition:

<script type="text/html" id="group-separator-template">
    <optgroup label="__________"></optgroup>
    <optgroup  data-bind="attr: {label: Text}"></optgroup>
</script>
<script type="text/html" id="element-template">
    <option data-bind="text: Text, value: Value"></option>
</script>

Select definition:

    <select data-bind="template: { name: pubCodeTemplate, foreach: PubCodes } , optionsCaption: 'Please Select', value:SelectedPubCode">
    </select>

And you need to add this code into your javascript model:

self.pubCodeTemplate = function (pubCode) {
        return pubCode.Type !== 1 ? 'element-template' : 'group-separator-template';
};

Upvotes: 4

Related Questions