【问题标题】:Selectbox with optgroup with knockoutJS带有 optgroup 和 knockoutJS 的选择框
【发布时间】:2012-09-30 17:25:57
【问题描述】:

我需要按类别显示项目组的选择框,如下所示:

我的 C# 模型是:

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(); }
    }
}

我的 Javascript 模型是:

<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>

我的 HTML 是

<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>

在除 IE 之外的所有浏览器中,我都达到了我需要的内容,但在 IE 中,它看起来像这样: 任何建议如何在 IE 中解决这个问题?

【问题讨论】:

    标签: c# select knockout.js


    【解决方案1】:

    您需要使用模板绑定,因为 IE 会从&lt;select&gt; 中删除 cmets。

    模板定义:

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

    您需要将此代码添加到您的 javascript 模型中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多