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 9070645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:43:59+00:00 2026-06-16T17:43:59+00:00

I have an observable array of Services that might be provided by a company

  • 0

I have an observable array of Services that might be provided by a company (i.e. Sales, Service, Consulting). Each of these services may be performed by the company in zero or more US States, and for each state there is an optional certification number. Thus, the Service has an observable array of Locations, and a Location is a combination of State and Cert Number.

function Service(name) {
    var self = this;

    self.Name = ko.observable(name);
    self.Locations = ko.observableArray();
}

function Location(state) {
    var self = this;

    self.State = ko.observable(state);
    self.CertNum = ko.observable();
}

In the user interface, I want to display a single list of all the US States. The user would tick the checkbox next to each state the in which the company operates, making a “master list” of states. This master list, in turn, drives the choices that appear under each of the possible services.

For example, the user ticks “Alabama” and “Arizona” in the master list. The UI then renders a separate Location for each Service as a combination of a checkbox and textbox. Here’s the master view model:

function viewModel() {
    var self = this;

    self.AllStates = [
        { name: "Alabama", abbrev: "AL" },
        { name: "Alaska", abbrev: "AK" },
        { name: "Arizona", abbrev: "AZ" },
        { name: "Arkansas", abbrev: "AR" }
    ];

    self.BusinessStates = ko.observableArray();

    self.AllSelectedStates = ko.computed( function() {
        return ko.utils.arrayFilter( self.AllStates, function(item) {
            return self.BusinessStates.indexOf(item.abbrev) >= 0;
        });
    });

    self.Services = ko.observableArray([
        new Service("Sales"),
        new Service("Service"),
        new Service("Consulting")
    ]);
}

A simple view would look like:

<h2>Master List</h2>
<ul data-bind="foreach: AllStates">
    <li>
        <label><input type="checkbox" data-bind="value: abbrev, checked: $root.BusinessStates"/> <span data-bind="text: name"></span></label></li>
</ul>

<h2>Services</h2>
<div class="service" data-bind="foreach: Services">
    <h3 data-bind="text: Name"></h3>
    <ul data-bind="foreach: $root.AllSelectedStates">
        <li>
           <label>
             <input type="checkbox" data-bind="value: abbrev, checked: $parent.Locations" /> 
             <span data-bind="text: name"></span>
           </label> 
           <input type="text" placeholder="Cert. Number" data-bind="value: ??" />
        </li>
    </ul>
</div>

That view at least gets me a state entered into the Service’s Locations array, but I don’t know how to bind the CertNum property.

Let’s say the user has selected Alabama and Arizona in the master list. Now, for the “Sales” service, the user ticks “Arizona” and enters a certification number of “98765”. Under the “Consulting” service, the user ticks “Alabama” and does not enter a cert number. Ideally I’d like the resulting JSON snippet to look like:

"Services": [
    {
        "Name": "Sales",
        "Locations": [
            {
                "State": { "name": "Arizona", "abbrev": "AZ" },
                "CertNum": "98765"
            }
        ]
    },
    {
        "Name": "Service",
        "Locations": []
    },
    {
        "Name": "Consulting",
        "Locations": [
            {
                "State": { "name": "Alabama", "abbrev": "AL" },
                "CertNum": null
            }
        ]
    }
]

I can render and bind the “master list” of US States. I can iterate over the services and render a UI that provides controls for each available service location (i.e. checkbox for the state, textbox for the cert. number). What I haven’t been able to is bind the service location controls to control membership of the location in the appropriate Locations array, AND affect the value of the certification number at the same time.

The closest attempt I’ve made is pretty complicated and hacky, where I’m using the knockout-postBox plugin to link the viewModel.AllSelectedStates property to a similar property on each Service model, and then push new Locations into each Service.Locations array. It almost works, except it wipes out changes to the Services whenever a state is checked or unchecked in the master list. Also, I have to push every available location into the Locations array for each Service, and rely on an IsSelected boolean flag on the location to determine whether it’s been selected (I’d rather put just the locations in the array that were selected).

You can see the attempt in this JSFiddle: http://jsfiddle.net/cbono/PU6Sq/23/

The nice thing about it is it demonstrates how the UI should behave. But I think this is possible without the postBox plugin, I’m just not looking at the problem the right way to work out the answer. I’ve spent several days working through this and can’t come up with a solution that works 100%.

  • 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-16T17:44:00+00:00Added an answer on June 16, 2026 at 5:44 pm

    It is easier to use the visible-binding, than to manage the collection events.

    <li data-bind="visible: State().IsSelected">
    

    http://jsfiddle.net/MizardX/XTkpb/


    If you really need to handle them, you could cache the StateAndCert objects in the Services objects:

    var self = this,
        cache = {};
    
    self.Locations = ko.computed(function () {
        return ko.utils.arrayMap(states(), function (item) {
            var key = item.Abbrev();
            if (cache[key] === undefined) {
                cache[key] = new StateAndCert(item);
            }
            return cache[key];
        });
    });
    

    http://jsfiddle.net/MizardX/HMAtE/


    To hide certain properties from the JSON rendering, you could stuff them inside a function(){} property:

    self.Meta = function(){};
    self.Meta.IsSelected = ko.observable(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Knockout observable array that I wish to edit from within Javascript
I have an observable array that contains a nested tree array. Every array item
I have an observable array that holds a list of people and a table
I have an observable array containing objects (dynamically created) that have themselves observable attributes.
I have a Knockout.JS observable array that is the basis for a list: self.list_elements
i have a an observable array in my model that is bind to just
I have an observable array with var items= [ {image: /images/image1.jpg }, {image: /images/image2.jpg
I am new to Knockout.js, I have created an observable array and initialized with
I have a an array of observable collections and I want to display all
I have an observable collection of objects that I'd like to display on the

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.