Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:11:58+00:00 2026-06-17T21:11:58+00:00

I’m fairly new to knockout.js, however, I’ve been happily using it in my ASP.NET

  • 0

I’m fairly new to knockout.js, however, I’ve been happily using it in my ASP.NET MVC 4 project, until I ran into this obstacle which has been bothering me for a while, can’t seem to put my finger on it.

The scenario which I’m working on requires several combinations of location data (region, country, city), i.e. cascading dropdown lists, which isn’t a problem to do when inputting fresh data, but I ran into problem(s) when trying to edit the saved data.

Data is in JSON format, with nested arrays, looks like this (shortened for illustration purposes):

var newData = 
[
  {
    "ID":1,
    "Name":"Australia and New Zealand",
    "Countries":[
      {
        "ID":13,
        "Name":"Australia",
        "Cities":[
          {
            "ID":19,
            "Name":"Brisbane"
          },
          {
            "ID":28,
            "Name":"Cairns"
          },
...

I suspect I can’t load the data (or more clearly, to bind it) properly since I’m having trouble accessing the Region sub-array (which contains Region’s Countries) and the Countries sub-array (which contains Countries’ Cities).

Then there’s the matter of having prepopulated options, which works partially, the viewmodel loads the number of lines, but doesn’t select anything.

Here’s the VM:

   var existingRows = [
    {
        "Region": 1,
        "Country": 13,
        "City": 19
    },
    {
        "Region": 1,
        "Country": 158,
        "City": 3
    }];

   var Location = function (region, country, city) {
       var self = this;
       self.region = ko.observable(region);
       self.country = ko.observable(country);
       self.city = ko.observable(city);

       // Whenever the region changes, reset the country selection
       self.region.subscribe(function () {
           self.country(undefined);
       });

       // Whenever the country changes, reset the city selection
       self.country.subscribe(function () {
           self.city(undefined);
       });
   };

   var LocationViewModel = function (data) {
       var self = this;

       self.lines = ko.observableArray(ko.utils.arrayMap(data, function (row)
       {
           var rowRegion = ko.utils.arrayFirst(newData, function (region)
           {
               return region.ID == row.Region;
           });
           var rowCountry = ko.utils.arrayFirst(rowRegion.Countries, function (country) {
               return country.ID == row.Country;
           });
           var rowCity = ko.utils.arrayFirst(rowCountry.Cities, function (city) {
           return city.ID == row.City;
           });
           return new Location(rowRegion, rowCountry, rowCity);
       }));

       // Operations
       self.addLine = function () {
           self.lines.push(new Location())
       };
       self.removeLine = function (line) {
           self.lines.remove(line)
       };
   };

   var lvm = new LocationViewModel(existingRows);

   $(function () {
       ko.applyBindings(lvm);
   });

HTML code:

<tbody data-bind="foreach: lines">
    <tr>
        <td><select data-bind="options: newData, optionsText: 'Name', optionsValue: 'ID', optionsCaption: 'Select a region...', attr: { name: 'SubRegionIndex' + '['+$index()+']' }, value: region"></select></td>
        <td><select data-bind="options: Countries, optionsText: 'Name', optionsValue: 'ID', optionsCaption: 'Select a country...', attr: { name: 'CountryIndex' + '['+$index()+']' }, value: country"></select></td>
        <td><select data-bind="options: Cities, optionsText: 'Name', optionsValue: 'ID', optionsCaption: 'Select a city...', attr: { name: 'CityIndex' + '['+$index()+']' }, value: city"></select></td>
        <td><a href='#' data-bind='click: $parent.removeLine'>Remove</a></td>
    </tr>    
</tbody>

I tried to modify the Cart editor example from the knockout.js website with prepopulated data, but haven’t really made much progress, I seem to be missing something. Didn’t really find anything with nested arrays so I’m stuck here…

I’ve put up the full code on JSFiddle here:
http://jsfiddle.net/fgXA2/1/

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:11:59+00:00Added an answer on June 17, 2026 at 9:11 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am using JSon response to parse title,date content and thumbnail images and place
this is what i have right now Drawing an RSS feed into the php,
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.