knockout.js - nested array data and cascading pre-populated dropdown lists binding

谁说我不能喝 提交于 2019-12-04 10:15:18

The problem is the way in which you are binding to the selected item in your select lists:

<select data-bind="
    options: newData, 
    optionsText: 'Name', 
    optionsValue: 'ID', 
    value: region">
</select>

Here you are binding the ID property from your JSON data to the region property on your view model.

This means that when you bind your second select list:

<td data-bind="with: region">
    <select data-bind="
        options: Countries, 
        optionsText: 'Name', 
        optionsValue: 'ID', 
        value: $parent.country">
    </select>
</td>

You attempt to bind to region.Countries. However, region simply contains the selected region ID. In this case the console is your friend:

Uncaught Error: Unable to parse bindings. Message: ReferenceError: Countries is not defined;

The same problem is true of your third select list for Cities since you are now attempting to bind to country.Cities where country is also just the ID.

There are two options available here. The first is to remove the optionsValue parameters, thus binding the actual JSON objects to your view model properties. That and a binding error on your Cities select box (you were binding to CityName instead of Name) were the only problems:

http://jsfiddle.net/benfosterdev/wHtRZ/

As you can see from the example I've used the ko.toJSON utility to output your view model's object graph. This can be very useful in resolving problems (in your case you would have seen that the region property was just an number).

The downside of the above approach is that you end up storing a copy of all of the countries, and their cities for the selected country in your view model.

A better solution if dealing with large data sets would be to only store the selected identifier (which I believe you were attempting to do originally) and then define computed properties that filter your single data set for the required values.

An example of this can be seen at http://jsfiddle.net/benfosterdev/Bbbt3, using the following computed properties:

    var getById = function (items, id) {
        return ko.utils.arrayFirst(items, function (item) {
            return item.ID === id;
        });
    };

    this.countries = ko.computed(function () {
        var region = getById(this.selectedRegion.regions, this.selectedRegion());
        return region ? ko.utils.arrayMap(region.Countries, function (item) {
            return {
                ID: item.ID,
                Name: item.Name
            };
        }) : [];
    }, this);

    this.cities = ko.computed(function () {
        var region = getById(this.selectedRegion.regions, this.selectedRegion());
        if (region) {
            var country = getById(region.Countries, this.selectedCountry());
            if (country) {
                return country.Cities;
            }
        }

    }, this);

You can see from the rendered object graph that only the currently selected countries and cities are copied to the view model.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!