0

我有以下场景,我使用 ko.mapping 从服务器获取一些数据,并以以下形式翻译:

var viewModel = {
    name: "Abc",
    educations: [{ course: "C1", countryID: 1, cityID = 2},
                 { course: "C2", countryID: 2, cityID = 3}]
}

我还有 2 个数组变量,它们是:

var countries = [{id=1,name="UAE"},{id=2,name="USA"},];
var cities = [{id=1,name="Dubai", cid=1},{id=2,name="Al-Ain", cid=1},
              {id=3,name="Newyork", cid=2},{id=4,name="Houston", cid=2},];

现在要显示/编辑此数据,我有以下 HTML

<div>
    <input type="text" data-bind="value: Name"/>
    <table data-bind="template: { name: 'cet', foreach: educations }">
    </table>
</div>
<script type="text/html" id="cet">
    <tr>
        <td>
            <select data-bind="options: countries, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: countryID"></select>
        </td>
        <td>
            <select data-bind="options: cities, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: cityID"></select>
        </td>
    </tr>
</script>

现在我需要的是,当从服务器发送数据时,选择框应该显示与绑定对象对应的正确项目。

4

2 回答 2

0

你几乎达到了你的目标——是你的代码的工作版本。

var viewModel = {
    name: "Abc",
    educations: [{course: "C1", countryID: 1, cityID: 2},
                 {course: "C2", countryID: 2, cityID: 3}],
    countries: [{id: 1, name: "UAE"}, {id: 2, name: "USA"}],
    cities: [{id: 1, name: "Dubai", cid: 1},
             {id: 2, name: "Al-Ain", cid: 1},
             {id: 3, name: "Newyork", cid: 2},
             {id: 4, name: "Houston", cid: 2}]
};

ko.applyBindings(viewModel);
<div>
    <input type="text" data-bind="value: name" />
    <table data-bind="template: { name: 'cet', foreach: educations }"></table>
</div>
<script type="text/html" id="cet">
    <tr>
        <td>
            <select data-bind="options: $root.countries, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: countryID"></select>
        </td>
        <td>
            <select data-bind="options: $root.cities, optionsText: 'name', optionsValue: 'id', optionsCaption: 'Select...', value: cityID"></select>
        </td>
    </tr>
</script>
于 2013-02-21T18:04:22.443 回答
0

好的解决了:)

数据绑定应按如下方式完成:

<select id="Countries" name="Countries" data-bind="options: countries, optionsText: 'CountryName', optionsValue: 'CountryID', optionsCaption: 'Select...', value: SelectedCountryID"></select>
<select id="Cities" name="Cities" data-bind="options: Cities, optionsText: 'CityName', optionsValue: 'CityID', optionsCaption: 'Select...', value: CityID"></select>

首先,我必须运行ko.mapping将 viewModel 转换为“ Observable viewModel ”,然后代码将如下所示:

if(viewModel.educations() != undefined && viewModel.educations().length > 0) {
    for(k in viewModel.educations()) {
        var education = viewModel.educations()[k];

        education.Cities = ko.observableArray();
        education.SelectedCountryID = ko.computed({
            read: function() {
                return(this.CountryID());
            },
            write: function(value) {
                this.CountryID(value);
                this.Cities.removeAll();

                if(value != undefined) {
                    for(c in cities) {
                        if(cities[c].cid == value) {
                            this.Cities.push(cities[c]);
                        }
                    }
                }
            },
            owner: education,
        });

        if(viewModel.educations()[k].CountryID() != 0 ||
            viewModel.educations()[k].CountryID() != undefined) {
            viewModel.educations()[k].SelectedCountryID(viewModel.educations()[k].CountryID());
        }
    }
}
于 2013-02-24T14:52:15.427 回答