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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:41:59+00:00 2026-06-17T11:41:59+00:00

I am having issues getting a foreach loop to work in KnockOut.js and even

  • 0

I am having issues getting a foreach loop to work in KnockOut.js and even when I try to get the code to throw an error it does not. It is as if it is not trying to bind to the table at all.

var History = function (data) {
    this.Average = ko.observable(data.Average);
    this.Count = ko.observable(data.Count);
};

var DataSource = function (data) {
    var convertData = function (array) {
        return $.map(data.Data, function (option) {
            return new History;
        });
    };

    this.Data = ko.observableArray(this.convertData());

    return {
        'Data': this.Data
    };
};

var dataMappingOptions = {
    create: function (options) {
        return new DataSource(options);
    }
};

var dataModel = function () {
    this.source = ko.mapping.fromJS({ 'Data': [], 'Percentage': 0 });
};

currentViewModel = new dataModel();
ko.applyBindings(currentViewModel);

I have stripped out a lot of the complexity of this as everything else works. Basically I am using the Knockout.Mappings to map an AJAX response to these objects and adding functions for computed fields. Here is the HTML that seems to ignore everything I do; it will display, but nothing binds to it.

<table>
    <thead>
        <tr>
            <th>Average</th>
            <th>Frequency</th>
        </tr>
    </thead>
    <tbody data-bind="foreach: kjhkjh">
        <tr>
            <td data-bind="text: $data.Average()"></td>
            <td data-bind="value: $data.Average"></td>
            <td data-bind="text: $data.Average"></td>
            <td data-bind="value: $data.Average()"></td>
            <td>Test</td>
        </tr>
    </tbody>
</table>

Since the kjhkjh object doesn’t bind I would think it would throw an error, but it does not. Even when I put in source.Data it doesn’t do anything; it only displays the static table and nothing binds to it.

Update:
In order to get the value from anything in Javascript I have to use some thing like this:

console.log(currentViewModel.source.Data()[11].Average());
  • 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-17T11:42:01+00:00Added an answer on June 17, 2026 at 11:42 am

    The error I was having was caused by the placement of my Knockout model on the page. Once I moved the script tag referring to the JS file below where the table was it worked correctly. That is why all of the page worked except for this excerpt. This is in part because I have it as a global variable as multiple parts of the page and several JS files needed to reference it.

    That being said Stockedout did point out some ways I was able to improve the code.

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

Sidebar

Related Questions

Im having issues getting this to work, maybe its not even possible? I have
I'm having some issues getting file uploads to work correctly and the following code
I am having some issues with my code, and keep getting the same error,
I'm having issues getting arrays to stick within a foreach loop. $sites = array($siteOne,
I am having issues getting setCenter to work. My JS console tells me: myMap
I am having issues getting the -e and -d file test operators to work
I know it can be done but am having issues getting it to work.
I'm having some issues with getting cookies to work when using a ProxyPass to
I am having issues with my code below. Everything works fine for getting Added
having an issue getting this to work. MySQL keeps throwing the following: ERROR 1064

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.