无法获得foreach knockout.js中的最后一个索引

时间:2014-03-04 08:15:06

标签: knockout.js knockout-2.0 knockout-mvc

我无法在gsp页面中的foreach中获取最后一个observableArray索引,如下所示

<!-- ko foreach: $data.audienceInformation.IndivisualUsers -->
  <span data-bind="html:$index()"></span>        //Here index is coming correctly
  <span data-bind="html:$last"></span>           //i want last index here
  <span data-bind="html:$data+','"></span>
<!-- /ko -->

这是我的JSON数据

{
    "AccountMessagesHistory": {
        "audienceInformation": {
            "Accounts": ["DHL"],
            "IndivisualUsers": ["fashangxue8@163.com"]
        },
        "documentInformation": ["Name: ",
               "Title: <p>aa</p>",
               "Description: <p>aa</p>",
               "Document Type: text",
               "This document belongs (creator/last modifier) to: b2b:cpqadmin@compaq.com",
               "This document is published for the language: en_US",
               "This document was created on: Mon Mar 03 03:15:09 MST 2014",
               "This document was last modified on: Mon Mar 03 03:15:09 MST 2014",
               "This document is a text document",
               "This document will be available from: Mon Mar 03 00:00:00 MST 2014",
               "This document will be available until: Tue Mar 03 00:00:00 MST 2015",
               "This document is available under message feature card"],
        "documentHistory": ["User ID: b2b:cpqadmin@xyz.com      Name: HP Admin,ITG      Modified On: Mon Mar 03 03:15:09 MST 2014"]
    }
}

2 个答案:

答案 0 :(得分:2)

使用数组的$last代替.length

<span data-bind="text: ko.utils.unwrapObservable($parent.audienceInformation.IndivisualUsers).length - 1"></span>

答案 1 :(得分:1)

如果你使用的是淘汰赛2.1.0,你可以使用:

<span data-bind="text: $parent.audienceInformation.IndivisualUsers().length - 1"></span>

<span data-bind="text: ko.utils.unwrapObservable($parent.audienceInformation.IndivisualUsers).length - 1"></span>