Rectangle 27 0

javascript knockout how to subscribe for multiple dropdown menus (select) and bind to a model?


Thanks again! Diana, can you help with computing the total price for each contact and sum of all this totals? for example: Firtname: John, Lastname: Carter, Total Debt: 10 USD (sum price of services)... and overall total: 30 USD

You are binding all your services select elements to the same $root.selectedId value.That's why the corresponding subscribe is being fired upon any change in any of them.

You need to bind the value dynamically based on the contact record for example.

Note
Rectangle 27 0

javascript knockout how to subscribe for multiple dropdown menus (select) and bind to a model?


self.addContact = function () {
    self.contacts.push({
        firstName: "",
        lastName: "",
        services: ko.observableArray([new Catalog(serviceTypes)])
    });
 };

 self.addService = function (contact) {
    contact.services.push(new Catalog(serviceTypes));
 };
function Catalog(serviceTypes, d) {
   this.catalog = serviceTypes;
   this.selectedId = ko.observable(d || null);
   this.selectedId.subscribe(function (item) {
     //Subscriber Handler
   });
}
var initialData = [{
  firstName: "John",
  lastName: "Carter",
  services: [new Catalog(serviceTypes, 1), new Catalog(serviceTypes, 2)]
}];

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

Thanks for the answer Akhlesh, can you help with computing the total price for each contact and sum of all this totals? for example: Firtname: John, Lastname: Carter, Total Debt: 10 USD (sum price of services)

You are sharing same observable($root.selectedId) instead of this every catalog should have there own copy of selectedId. for that you can use constructor function eg,

addContact and addService function are also changed.

Note