Knockout.js 使用敲除在选择框中预选值

Knockout.js 使用敲除在选择框中预选值,knockout.js,Knockout.js,有谁能告诉我如何根据ajax调用返回的对象从下拉列表中预先选择一个值。我一直在学习淘汰赛教程,我自己也在尝试一些东西。它从不根据员工的职位对象在下拉列表中选择正确的值。下面是我的代码,这里是我的小提琴的链接 默认情况下,敲除选项绑定在对象上工作,这就是您使用它的方式。问题是positions数组的position对象与员工的position对象不同,即使它们的值相同,因为您正在为每个员工创建一个新的position对象 您必须选择如何存储该位置 您可以使用选项Value binding映射员工的

有谁能告诉我如何根据ajax调用返回的对象从下拉列表中预先选择一个值。我一直在学习淘汰赛教程,我自己也在尝试一些东西。它从不根据员工的职位对象在下拉列表中选择正确的值。下面是我的代码,这里是我的小提琴的链接

默认情况下,敲除选项绑定在对象上工作,这就是您使用它的方式。问题是positions数组的position对象与员工的position对象不同,即使它们的值相同,因为您正在为每个员工创建一个新的position对象

您必须选择如何存储该位置

您可以使用选项Value binding映射员工的职位id,如下所示

<div data-bind="foreach: employees">
    <div>
        <label>Full Name</label>
        <input data-bind="value: fullName" />
        <label>Position</label>
        <select id="slTest" data-bind="options: $root.Positions, value:position, optionsText:'name'"></select>
        <label>Salary:</label><span data-bind="text: position().salary"></span>
    </div>
</div>

function Employee(fullname, position) {
    var self = this;
    self.fullName = ko.observable(fullname);
    self.position = ko.observable(position);
}

function Position(id, name, salary) {
    var self = this;
    self.id = ko.observable(id);
    self.name = ko.observable(name);
    self.salary = ko.observable(salary);
}

function EmployeeVM() {
    var self = this;
    self.employees = ko.observableArray();

    self.Positions = [
    new Position(1, "No position", "0"),
    new Position(3, "Web Developer", "15100"),
    new Position(2, "Manager", "30000")];

    var data = {
        json: $.toJSON([{
            "fullName": "Richard Banks",
                "position": {
                    "id": 2,
                "name": "Manager",
                    "salary": "30000"
            }
        }, {
            "fullName": "Dave Grohl",
                "position": {
                    "id": 3,
                "name": "Web Developer",
                    "salary": "15100"
            }
        }, {
            "fullName": "bobby rahul",
                "position": {
                    "id": 3,
                "name": "Web Developer",
                    "salary": "15100"
            }
        }])
    };

    $.ajax({
        url: "/echo/json/",
        data: data,
        type: "POST",
        success: function (response) {
            $.each(response, function (i, item) {
                var e = new Employee(item.fullName, new Position(item.position.id, item.position.name, item.position.salary));
                self.employees.push(e);
            });

            //alert($("#slTest").html());
        }
    });
}

ko.applyBindings(new EmployeeVM());
<select id="slTest" data-bind="options: $root.Positions, value:position, optionsText:'name', optionsValue: 'id'"></select>
---
$.each(response, function (i, item) {
                var e = new Employee(item.fullName, item.position.id);
                self.employees.push(e);
            });
$.each(response, function (i, item) {
                var position  = ko.utils.arrayFirst(self.Positions, function(p) {
                   return p.id() == item.position.id; 
                });
                var e = new Employee(item.fullName, position);
                self.employees.push(e);
            });