结合使用基因敲除的foreach嵌套数组(Binding nested array using Kn

2019-09-23 10:25发布

我需要用基因敲除的foreach结合嵌套数组援助。

下面是代码,想知道我怎样才能得到这里面PatAppointments数组中的元素。

 <script language="javascript" type="text/javascript">
 ko.applyBindings({
    "appointment": [{
        "Date": "01\/10\/2012",
        "PatAppointments": [{
            "EndTime": "11:15:00",
            "EventId": null,
            "Facility": "General Physician",
            "PatientID": 23,
            "PatientName": "Vicki"
        }],
        "PatAppointments": [{
            "EndTime": "11:15:00",
            "EventId": null,
            "Facility": "General Physician",
            "PatientID": 23,
            "PatientName": "Scott"
        }]
    }]
});

</script>

<table>
<tbody data-bind="foreach: appointment">
<tr>
    <td data-bind="text: Date">
    </td>
</tr>
<tr>
    <td>
         @*
        <tbody data-bind="foreach: appointment.PatAppointments">
        <tr>
            <td data-bind="text: PatAppointments.PatientName">
            </td>
            <td data-bind="text: PatAppointments.Facility">
            </td>
        </tr>
        </tbody>
        *@
    </td>
</tr>
</tbody>
</table>

Answer 1:

正如您已经嵌套在另一个一个阵列时,需要在2嵌套html元素(DIV,UL,TR,...)等在下面的例子中,以限定2个的foreach绑定:

<div data-bind="foreach: appointment">
    <div data-bind="foreach: PatAppointments">
        <span data-bind="text: PatientName"></span>
    </div>
</div>


Answer 2:

当你有你有它设置目前,没有任何的foreach会工作。 要正确设置你的PatAppointments,你的对象应该像

"appointment": [{
    "Date": "01\/10\/2012",
    "PatAppointments": [
    {
        "EndTime": "11:15:00",
        "EventId": null,
        "Facility": "General Physician",
        "PatientID": 23,
        "PatientName": "Vicki"
    },
    {
        "EndTime": "11:15:00",
        "EventId": null,
        "Facility": "General Physician",
        "PatientID": 23,
        "PatientName": "Scott"
    }]
}]

然后为GBS曾表示,你会想foreach另一内结合foreach这样的结合:

<div data-bind="foreach: appointment">
    <div data-bind="foreach: PatAppointments">
        //Everything you want displayed from each PatAppointment here.
    </div>
</div>

见小提琴的小例子。



Answer 3:

我与嵌套阵列,其中很难/没用创建元素只是在foreach语法绑定工作。 我喜欢“无容器控制流语法”,看起来像这样:

<!-- ko foreach: appointment -->
    <!-- ko foreach: PatAppointments -->
        <span data-bind="text: PatientName"></span>
    <!-- /ko -->
<!-- /ko -->

看到它的文档,在“注4” http://knockoutjs.com/documentation/foreach-binding.html



文章来源: Binding nested array using Knockout foreach
标签: knockout.js