使用Knockout foreach绑定嵌套数组 [英] Binding nested array using Knockout foreach

查看:78
本文介绍了使用Knockout foreach绑定嵌套数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用敲除foreach绑定嵌套数组的帮助.

I need assistance in binding nested array using knockout foreach.

下面是代码,想知道如何获取PatAppointments数组中的元素.

Below is the code, would like to know how can I get the elements which is inside PatAppointments array.

 <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>

推荐答案

当一个数组嵌套在另一个数组中时,您需要在2个嵌套的html元素(div,ul, tr,...),如以下示例所示:

As you have one array nested in another, you need to define 2 foreach bindings in 2 nested html element (div, ul, tr, ...) like in the following example:

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

这篇关于使用Knockout foreach绑定嵌套数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆