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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:57:43+00:00 2026-06-17T00:57:43+00:00

Could someone assist in explaining the probably fundamental fault I am doing. I need

  • 0

Could someone assist in explaining the probably fundamental fault I am doing.
I need to be able to have a model which can group a list of objects each with its own set of bindings.

A fiddle shows what I what to do with an example of only 1 block but in the end I want multiple blocks.

Html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"
type="text/javascript"></script>
<div class="productblock">
  <div class="product">
    <input class="binding" data-bind="value: amount" type="text" value="1">
  </div>
  <div class="product">
    <input class="binding" data-bind="value: amount" type="text" value="2">
  </div>
  <input class="discount" data-bind="value: discount" value="50">
</div>

</div>
  <span id="res" data-bind="text: result">result here </span>

js

function ResultViewModel() {
  this.result = ko.computed(function () {
    return 1;
  });
}

function ProductViewModel(val) {
  this.amount = ko.observable(val+1);
  this.sum = function () {
    return this.amount();
  };
}

  var products = [];
function ProductBlockViewModel(block) {
  $.each($(block).find("div.product"), function (index, element) {
    var product = new ProductViewModel($(element).find("input").val());
    products.push(product);
    ko.applyBindings(product);
  });
  this.discount = ko.observable(22);

}
var productBlocks = [];
$.each($("div.productblock"), function (index, element) {
  console.log("a");
  var productBlock = new ProductBlockViewModel(element);
  productBlocks.push(productBlock);
  ko.applyBindings(productBlock, element);
  console.log("asdf");
});

ko.applyBindings(new ResultViewModel());
http://jsfiddle.net/Todilo/DGUKu/2/

  • 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-17T00:57:44+00:00Added an answer on June 17, 2026 at 12:57 am

    See demo: http://jsfiddle.net/DGUKu/7/

    I will re-structure your code to make it cleaner.

    HTML:

    <!-- ko foreach: productBlocks -->
    <div class="productblock">
       <!-- ko foreach: productLines -->
      <div class="product">
        <input class="binding" data-bind="value: amount" type="text">
      </div>
      <!-- /ko -->
      <input class="r" data-bind="value: discount" value="50">
    </div>
        <hr/>
    <!-- /ko -->
    
    <div>
    <span id="res" data-bind="text: result"></span>
    </div>
    

    JS:

    function ProductModel (){
      this.amount = ko.observable(0);
      this.sum = function () {
        return this.amount();
      };
    }
    
    // product block contains multiple product line
    function ProductBlockViewModel(productArray) {
      this.productLines = ko.observableArray(productArray);
      this.discount = ko.observable(50);
    }
    
    // your viewmodel for binding data
    // should call applybindings only one
    function ViewModel(){
      this.result = ko.observable(0);
      this.productBlocks = ko.observableArray([]);
    }
    
    var viewModel = new ViewModel();
    
    // let assume you have 2 blocks:
    
    // block 2 products
    viewModel.productBlocks.push(new ProductBlockViewModel([
      new ProductModel(), new ProductModel()
    ]));
    
    // block 3 products
    viewModel.productBlocks.push(new ProductBlockViewModel([
      new ProductModel(), new ProductModel(), new ProductModel()
    ]));
    
    // you can calculate your total result here
    viewModel.result(100);
    
    ko.applyBindings(viewModel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Was wondering if someone could assist with some Postgres. I have a table which
Hope someone can assist with this, it's driving me crazy. I have a class
Just wondering if someone can assist me with the following issue. I have a
could someone please tell me what I need to do in order to create
Would someone kindly assist me with the following? I have two DataGridView objects that
could someone help me, i need a php statement that will display an image
I am trying to develop a mail trigger. Could someone assist on how this
I'm hoping someone can assist me with understanding if/how something like this is possible.
Could someone assist me with a bit of code I'd like to build to
Could someone assist me in helping me display more than 1/2 results? Here is

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.