). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
). , , knockout.
!
( ). .. , () .
( )
:
javascript
:
function OrgType(idv, namev, subTypesv){ var id = ko.observable(idv), name = ko.observable(namev), subTypes = ko.observableArray(subTypesv); return { id : id, name : name, subTypes : subTypes }; }
組織のサブタイプのモデル:
function OrgSubType(idv, namev){ var id = ko.observable(idv), name = ko.observable(namev); return { id : id, name : name }; }
そして、フォームに表示するためのメイン(メイン)モデル:
function OrgTypesModel(){ var orgTypes = ko.observableArray([ new OrgType(1,'tp1', [new OrgSubType(1,'subT1'), new OrgSubType(2,'subT2'), new OrgSubType(3,'subT3')]), new OrgType(2,'tp2', [new OrgSubType(4,'subT4'), new OrgSubType(5,'subT5'), new OrgSubType(6,'subT6'), new OrgSubType(7,'subT7')])]), selectedOrgTypeId = ko.observable(1), selectedOrgType = ko.computed(function(){ var otList = orgTypes(), selId = selectedOrgTypeId(); for(var i =0; i<otList.length; i++){ if(otList[i].id()==selId) return otList[i]; } return null; }), selectedOrgSubTypeId = ko.observable(1), selectedOrgSubType = function(){ var otList = selectedOrgType().subTypes(), selId = selectedOrgSubTypeId(); for(var i =0; i<otList.length; i++){ if(otList[i].id()==selId) return otList[i]; } return null; }; return { orgTypes : orgTypes, selectedOrgTypeId : selectedOrgTypeId, selectedOrgType : selectedOrgType, selectedOrgSubTypeId : selectedOrgSubTypeId, selectedOrgSubType : selectedOrgSubType }; }
- したがって、メインモデルはorgTypesプロパティを通じて組織タイプのリストを提供します
- selectedOrgTypeIdプロパティには、選択した組織タイプの識別子値が格納されます
- selectedOrgTypeプロパティから、組織タイプのオブジェクトを取得します*
- selectedOrgSubTypeIdを使用して、選択された組織のサブタイプの識別子を取得します
- selectedOrgSubTypeプロパティを使用して-組織のサブタイプのオブジェクトを取得します
HTMLコード
<div> <label>Organization type:</label> <select data-bind="options: orgTypes, optionsText: 'name', optionsValue: 'id', value: selectedOrgTypeId"> </select> <span data-bind="text: selectedOrgType().name"></span> </div> <div> <label>Organization sub type:</label> <select data-bind="options: selectedOrgType().subTypes, optionsText: 'name', optionsValue: 'id', value: selectedOrgSubTypeId"></select> <span data-bind="text: selectedOrgSubType().name"></span> </div>