我想创建一个表,其中我有两列,每列都有下拉菜单。第二列的下拉列表取决于第一列,但我无法使dependentObservable
下面是我的代码
<table>
<thead>
<tr><th>First</th><th>Last</th></tr>
</thead>
<tbody data-bind="foreach: List">
<tr>
<td><select data-bind="options: stateList, optionsValue: 'id' ,optionsText: 'name', value: stateId, optionsCaption: 'Choose...'"></select></td>
<td ><select data-bind="options: cities, optionsCaption: 'Choose...'"></select></td>
</tr>
</tbody>
js
states=[{id:1,name:"MP"},
{id:2,name:"UP"},
{id:3,name:"MH"},
{id:4,name:"AP"},
{id:5,name:"KP"}
];
city=[{cityid:1,name:"Indore"},
{cityid:1,name:"Gwalior"},
{cityid:1,name:"Bhopal"},
{cityid:2,name:"Jhansi"},
{cityid:2,name:"Kanpur"},
{cityid:2,name:"Merut"},
{cityid:3,name:"Pune"},
{cityid:3,name:"Mumbai"},
{cityid:4,name:"Hyderbad"},
{cityid:5,name:"Banglore"},
{cityid:5,name:"Mysore"}
];
viewModel=function(){
self=this;
self.state=ko.observableArray(states);
self.stateId=ko.observable();
self.cityId=ko.observable();
self.cityList=ko.observableArray(city);
self.SelectedCityList=ko.dependentObservable(function(){
var state=this.stateId();
var arr=[];
if(state){
ko.utils.arrayForEach(this.cityList(),function(item){
if(item.cityid===state){
arr.push(item);
}
});
}
return arr;
},this);
self.List=ko.observableArray([{stateList:ko.observableArray(self.state()),cities:self.SelectedCityList,cityList:ko.observableArray(self.cityList()),stateId:ko.observable(),cityId:ko.observable()},
{stateList:ko.observableArray(self.state()),cities:this.SelectedCityList,cityList:ko.observableArray(self.cityList()),stateId:ko.observable(),cityId:ko.observable()}
]);
}
ko.applyBindings(new viewModel());
请帮帮我这是小提琴链接点击这里