2

i have two classes; users and readings. this relates to healthcare to each user has an array of readings:

/*
 * Reading class
 * containts health readings
 */
function Reading(date,weight,glucose)
{
    var self = this;

    self.date=ko.observable(date);
    self.weight=ko.observable(weight);
    self.glucose=ko.observable(glucose);

      self.formattedWeight = ko.computed(function(){
        var formatted = self.weight();

        return formatted+" lb"
    });
}

/*
 * User class
 * contains a user name, date, and an array or readings
 */
function User(name,date,readings) {
    var self = this;

    self.name = name;
    self.date = date;
    self.readingsArray = ko.observableArray([
        new Reading(99,99)
    ]); 
}

i know how to use a foreach binding to display the information for a reading or a user. but im not sure how to show the readings inside of a user?

is there a way to use a nested foreach binding or a with binding? here is my html:

<h2>Users (<span data-bind="text: users().length"></span>)</h2>

<table>
    <thead><tr>
        <th>User name</th><th>Date</th></th>
    </tr></thead>
    <!-- Todo: Generate table body -->
      <tbody data-bind="foreach: users">
     <tr>
        <td><input data-bind="value: name" /></td>
        <td><input data-bind="value: date" /></td>
        <td data-bind="text: readingsArray"></td>
        <td><a href="#" data-bind="click: $root.removeUser">Remove</a></td>               
    </tr>
    </tbody>
</table>

<button data-bind="click: addUser">Add User</button>

<h2>Readings (<span data-bind="text: readings().length"></span>)</h2>
    <table>
    <thead><tr>
       <th>Date</th><th>Weight</th><th>Glucose</th>
    </tr></thead>
    <!-- Todo: Generate table body -->
      <tbody data-bind="foreach: readings">
     <tr>
        <td strong data-bind="text: date"></td>
        <td strong data-bind="text: formattedWeight"></td>
        <td strong data-bind="text: glucose"></td>
    </tr>
    </tbody>
</table>

and here is my model if anyone is interested. any help would be greatly appreciated! Thanks in advance!

// Overall viewmodel for this screen, along with initial state
function userHealthModel() {
    var self = this;
    self.inputWeight = ko.observable();
    self.inputDate = ko.observable();
    self.inputId = ko.observable();
    self.inputGlucose = ko.observable();

    // Operations
    self.addUser = function(){
        self.users.push(new User("",0,0,(new Reading(0,0))));
    }
    //adds a readings to the edittable array of readings (not yet the reading array in a user)
    self.addReading = function(){
        var date = self.inputDate();
        var weight = self.inputWeight();
        var glucose = self.inputGlucose();
        if((weight&&date)||(glucose&&date))
        {
            self.readings.push(new Reading(date,weight,glucose));
        }
        else{
            alert("Please complete the form!")
        }
    }

    self.removeUser = function(userName){self.users.remove(userName)}

    //editable data
     self.readings = ko.observableArray([
        new Reading(12,99,3),new Reading(22,33,2),
        new Reading(44,55,3)
    ]);

      self.users = ko.observableArray([
        new User("George",2012),
        new User("Bindu",2012)
    ]);

}

ko.applyBindings(new userHealthModel());

1 Answer 1

2

I'm not sure how you want the Readings rendered, but here is an example:

http://jsfiddle.net/jearles/aZnzg/

You can simply use another foreach to start a new binding context, and then render the properties as you wish.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry i should have been more specific. i meant the reading array inside the user class
Yes, that is what my example renders. It displays the Readings that are associated with the User. Notice in my fiddle that it displays '99 | 99' as that is what you have assigned to the Reading.
wow thanks i must have over looked that! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.