淘汰赛主细节不选择工作(Knockout master detail not working wit

2019-07-04 20:32发布

here`see my fiddle

http://jsfiddle.net/kirannandedkar/JUChh/2/

当我的名字点击它不加载选择列表,并给出errror该功能的预期。 其实我想加载选择和价值应该被选中。 当我选择改变的东西就应该得到反映在列表中。 目前的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>

Answer 1:

模块可观察到的数组包含没有观测简单的JS对象,以便访问其属性,当你不需要把() 从data.Id中删除它selectPerson功能:

selectPerson = function(p){
    selectedPerson(p);
     editModuleId(ko.utils.arrayFirst(modules(), function (data) {
            console.log("item module id " + p.ModuleId());
            return data.Id === p.ModuleId();
        }));
};

这里是工作提琴: http://jsfiddle.net/JUChh/3/

我看你的代码更近,发现editModuleId是多余的。 你还需要内部财产selectedPerson对象- ModuleId ,并应结合下拉的价值吧。

这里被重构小提琴: http://jsfiddle.net/JUChh/18/



文章来源: Knockout master detail not working with select
标签: knockout.js