简体   繁体   English

Knockout.js数据绑定数组到复选框列表

[英]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? 如何根据受伤的子阵列为每位患者选择多个复选框?

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 . 我想你在某个地方应该有一份列出所有可能的伤害,比如将在父视图模型中(以及患者的集合)并称为AllInjuries你还需要在你的Patient类中添加一些方法来迭代所有患者受伤并将确定该患者是否有这种伤害,让我们称之为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 -->

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM