首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Knockoutjs使用选项组选择

Knockoutjs使用选项组选择
EN

Stack Overflow用户
提问于 2012-01-23 21:27:58
回答 4查看 10.9K关注 0票数 19

在Knockoutjs绑定中有没有什么方法可以指定optionsGroup?像下面这样的东西

代码语言:javascript
复制
<select data-bind="options: collection, optionsText: 'Text', optionsGroup: 'Group'/>

请务必回复。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-15 19:55:01

我得到了同样的答案,如果有人想要,这就是答案,

代码语言:javascript
复制
<td><select class="fieldValue" data-bind="foreach: $root.AvailableFields, value: FieldId, event :{ change: $root.onFieldSelectionChange}">
        <optgroup data-bind="attr: {label: FieldGroupName}, foreach: Fields">
            <option data-bind="text: HeaderText, value: FieldId"></option>
        </optgroup>
     </select>
  </td>
票数 31
EN

Stack Overflow用户

发布于 2015-06-15 14:55:28

在许多情况下,您不需要选项本身是可观察的,只需要选择的结果。以下是进行英国县选择的示例。

HTML:

代码语言:javascript
复制
<div class="form-group">
    <label for="county">County</label>
        <select class="fieldValue" data-bind="foreach: $root.countyList, value: orgCounty">
            <optgroup data-bind="attr: {label: label}, foreach: counties">
            <option data-bind="text: label, value: label"></option>
            </optgroup>
        </select>
</div>

JS:

代码语言:javascript
复制
viewModel.countyList = getCountyList();
viewModel.orgCounty = ko.observable('London'); // Put default value here

function getCountyList() {
    var $arrayCounties = [
        { "label" : "England", "counties" : [ 
            { "label" : "Bedfordshire"},
            { "label" : "Berkshire"},
            { "label" : "Bristol"},
            { "label" : "Buckinghamshire"},
            { "label" : "Cambridgeshire"},
            { "label" : "Cheshire"},
            { "label" : "City of London"},
            { "label" : "Cornwall"},
            { "label" : "Cumbria"},
            { "label" : "Derbyshire"},
            { "label" : "Devon"},
            { "label" : "Dorset"},
            { "label" : "Durham"},
            { "label" : "East Riding of Yorkshire"},
            { "label" : "East Sussex"},
            { "label" : "Essex"},
            { "label" : "Gloucestershire"},
            { "label" : "Greater London"},
            { "label" : "Greater Manchester"},
            { "label" : "Hampshire"},
            { "label" : "Herefordshire"},
            { "label" : "Hertfordshire"},
            { "label" : "Isle of Wight"},
            { "label" : "Kent"},
            { "label" : "Lancashire"},
            { "label" : "Leicestershire"},
            { "label" : "Lincolnshire"},
            { "label" : "Merseyside"},
            { "label" : "Norfolk"},
            { "label" : "North Yorkshire"},
            { "label" : "Northamptonshire"},
            { "label" : "Northumberland"},
            { "label" : "Nottinghamshire"},
            { "label" : "Oxfordshire"},
            { "label" : "Rutland"},
            { "label" : "Shropshire"},
            { "label" : "Somerset"},
            { "label" : "South Yorkshire"},
            { "label" : "Staffordshire"},
            { "label" : "Suffolk"},
            { "label" : "Surrey"},
            { "label" : "Tyne and Wear"},
            { "label" : "Warwickshire"},
            { "label" : "West Midlands"},
            { "label" : "West Sussex"},
            { "label" : "West Yorkshire"},
            { "label" : "Wiltshire"},
            { "label" : "Worcestershire"} ]},
        { "label" : "Wales", "counties" : [ 
            { "label" : "Anglesey"},
            { "label" : "Brecknockshire"},
            { "label" : "Caernarfonshire"},
            { "label" : "Carmarthenshire"},
            { "label" : "Cardiganshire"},
            { "label" : "Denbighshire"},
            { "label" : "Flintshire"},
            { "label" : "Glamorgan"},
            { "label" : "Merioneth"},
            { "label" : "Monmouthshire"},
            { "label" : "Montgomeryshire"},
            { "label" : "Pembrokeshire"},
            { "label" : "Radnorshire"} ]},
        { "label" : "Scotland", "counties" : [ 
            { "label" : "Aberdeenshire"},
            { "label" : "Angus"},
            { "label" : "Argyllshire"},
            { "label" : "Ayrshire"},
            { "label" : "Banffshire"},
            { "label" : "Berwickshire"},
            { "label" : "Buteshire"},
            { "label" : "Cromartyshire"},
            { "label" : "Caithness"},
            { "label" : "Clackmannanshire"},
            { "label" : "Dumfriesshire"},
            { "label" : "Dunbartonshire"},
            { "label" : "East Lothian"},
            { "label" : "Fife"},
            { "label" : "Inverness-shire"},
            { "label" : "Kincardineshire"},
            { "label" : "Kinross"},
            { "label" : "Kirkcudbrightshire"},
            { "label" : "Lanarkshire"},
            { "label" : "Midlothian"},
            { "label" : "Morayshire"},
            { "label" : "Nairnshire"},
            { "label" : "Orkney"},
            { "label" : "Peeblesshire"},
            { "label" : "Perthshire"},
            { "label" : "Renfrewshire"},
            { "label" : "Ross-shire"},
            { "label" : "Roxburghshire"},
            { "label" : "Selkirkshire"},
            { "label" : "Shetland"},
            { "label" : "Stirlingshire"},
            { "label" : "Sutherland"},
            { "label" : "West Lothian"},
            { "label" : "Wigtownshire"} ]},
        { "label" : "Northern Ireland", "counties" : [ 
            { "label" : "Antrim"},
            { "label" : "Armagh"},
            { "label" : "Down"},
            { "label" : "Fermanagh"},
            { "label" : "Londonderry"},
            { "label" : "Tyrone"} ]}
        ];

    return $arrayCounties;
}

您将在viewModel.countyList中检索选定的选项。

票数 6
EN

Stack Overflow用户

发布于 2017-03-30 18:38:07

这还将允许使用占位符:

代码语言:javascript
复制
<select class="form-control needsclick" data-bind="value: Value">
  <option value="" disabled selected>Select Value...</option>
  <!-- ko foreach: Group -->
    <optgroup data-bind="attr: { label: Label }, foreach: Collection">
      <option data-bind="text: Label, value: $data"></option>
    </optgroup>
  <!-- /ko -->
</select>

JS的格式如下:

代码语言:javascript
复制
self.Group = ko.pauseableComputed(function () {
   var groups = [
     { 
       Label: 'Group 1',
       Collection: [
          { Label: 'Data 1', Value: 'Data 1' },
          { Label: 'Data 2', Value: 'Data 2' },
       ]
     },
     { 
       Label: 'Group 2',
       Collection: [
          { Label: 'Data 3', Value: 'Data 3' },
          { Label: 'Data 4', Value: 'Data 4' },
       ]
     },
   ]
   return groups;
})

在这个简单的实例中,此选项值可以是值。

代码语言:javascript
复制
<option data-bind="text: Label, value: Value"></option>

但对于更复杂的对象,$data将是更好的选择。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8972367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档