ノックアウトを使用する例として、2つのドロップダウンリストを使用して依存データを表示する

この投稿では、データを操作するためのノックアウトの基本機能をドロップダウンリスト(タグ
 ).      ,       ,     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 }; }
      
      











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>
      
      








All Articles