user4154355
user4154355

Reputation:

Knockout.js data-bind array to list of checkboxes

Patient 1
    Patient.Age = '25'
    Patient.Injury[0].Date = '2015-01-01'
    Patient.Injury[0].Type = 'Burned'
    Patient.Injury[1].Date = '2015-01-27'
    Patient.Injury[1].Type = 'Sprained Ankle'

Patient 2
    Patient.Age = '17'
    Patient.Injury[0].Date = '2015-01-08'
    Patient.Injury[0].Type = 'Papercut'

<!-- ko foreach: Patient -->
<input type="checkbox" data-bind="checked: ?"> Sprained Ankle
<input type="checkbox" data-bind="checked: ?"> Burned
<input type="checkbox" data-bind="checked: ?"> Papercut
<!-- /ko -->

Looping through an array of multiple patients and displaying a list of checkboxes of injury types of each patient. How do I select multiple checkboxes per patient, based off of sub-array of injuries?

Upvotes: 1

Views: 207

Answers (1)

Vladimirs
Vladimirs

Reputation: 8599

I suppose that you somewhere should have a list of all possible injuries let say that will be in a parent view model (along to collection of patients) and called AllInjuries also you will need to add some method to your Patient class what will iterate through all patient's injuries and will determine if that patient has that injury, let say it called hasInjury. Then you will be able to use something like this:

<!-- ko foreach: Patient -->
    <!-- ko foreach: $parents[1].AllInjuries -->
        <input type="checkbox" data-bind="checked: $parent.hasInjury($data.Type)">
        <span data-bind="text: Type"></span>
    <!-- /ko -->
<!-- /ko -->

Upvotes: 2

Related Questions