here`see my fiddle
http://jsfiddle.net/kirannandedkar/JUChh/2/
当我单击名称时,它不会加载选择列表并给出预期功能的错误。实际上我想加载选择并且应该选择值。当我在选择中更改某些内容时,它应该会反映在列表中。目前 ModuleId 没有被填充到列表中,当我点击列表时它没有填充选择列表。
视图模型:
var Person = function(id, name, country,ModuleId) {
var self = this;
self.id = ko.observable(id);
self.name = ko.observable(name);
self.country = ko.observable(country);
self.ModuleId= ko.observable(ModuleId);
return self;
};
var vm = (function() {
var people = ko.observableArray(),
selectedPerson = ko.observable();
self.editModuleId = ko.observable();
self.modules = ko.observableArray([{"Id": 1,"ModuleName": "M1"},{"Id":2,"ModuleName":"M2"}]);
getPeople = function() {
people.push(new Person(1, 'John', 'USA',1));
people.push(new Person(2, 'Mike', 'UK',1));
people.push(new Person(3, 'Dan', 'AUS',2));
},
selectPerson = function(p){
selectedPerson(p);
self.editModuleId(ko.utils.arrayFirst(self.modules(), function (data) {
console.log("item module id " + p.ModuleId());
return data.Id() === p.ModuleId();
}));
};
getPeople();
return {
people: people,
selectedPerson: selectedPerson,
selectPerson : selectPerson
};
})();
ko.applyBindings(vm);
看法:
<ul data-bind="foreach: people">
<li data-bind="text:name, click:$parent.selectPerson"></li>
<li data-bind="text:$root.editModuleId,click:$parent.selectPerson"></li>
</ul>
<div data-bind="with:selectedPerson">
<span data-bind="text:id"></span>
<input data-bind="value:name"/>
<input data-bind="value:country"/>
<select data-bind = "options:$root.modules,value:$root.editModuleId,optionsText:'ModuleName'"/>
</div>