0

嗨,我有一种情况,我有一个可观察数组的可观察数组。这就是我的标记的样子:

<div class="slider-wrap cycle-slideshow" data-cycle-fx="scrollHorz" data-cycle-timeout="0" data-cycle-slides="> section" data-cycle-prev=".slider-nav .prev" data-cycle-next=".slider-nav .next" data-cycle-pager=".slider-bullets" data-bind="foreach: category().questions()">
    <section data-bind="foreach: $data">   
        <article>
            <!-- ko if: hasGrade-->
            <header data-bind="text: $data.description"></header>
            <ul>
                <li><a href="#">1</a></li>
                <li><a href="#">2</a></li>
                <li><a href="#">3</a></li>
                <li><a href="#">4</a></li>
                <li><a href="#">5</a></li>
                <li><a href="#">6</a></li>
                <li><a href="#">7</a></li>
                <li><a href="#">8</a></li>
                <li><a href="#">9</a></li>
                <li><a href="#">10</a></li>
            </ul>
            <span data-bind="text: $parent.$index"></span>
            <!-- /ko -->
            <!-- ko if: hasMemo-->
            <header data-bind="text: $data.memoTitle"></header>
            <textarea></textarea>
            <!-- /ko -->
        </article>
        <!-- /ko -->
        <!-- ko if: $parent.$parent.hasMemo-->
        <article>
            <header data-bind="text: $parent.$parent.memoTitle"></header>
            <textarea></textarea>
        </article>
        <!-- /ko -->
    </section>
</div>

由于我正在使用一个插件并且没有不使用它的奢侈,我无法更改标记的结构。

最后一个 if 条件只应在迭代最后一个元素时检查。这是模型:

   {
memo: null,
categoryId: 1,
title: "Docent",
hasMemo: true,
memoIsMandatory: false,
memoTitle: "Docent Opmerkingen",
questions: [
[
{
memo: null,
grade: null,
description: "De docent is goed voorbereid",
memoTitle: "De docent is goed voorbereid",
questionId: 11,
hasGrade: false,
hasMemo: true,
showOnlyMemo: true
},
{
memo: null,
grade: null,
description: "De docent heeft kennis van zaken",
memoTitle: "De docent heeft kennis van zaken",
questionId: 12,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "De docent kan de onderwerpen boeiend uitleggen",
memoTitle: "De docent kan de onderwerpen boeiend uitleggen",
questionId: 13,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "De docent gaat goed in op de vragen uit de groep",
memoTitle: "De docent gaat goed in op de vragen uit de groep",
questionId: 14,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "De docent stimuleert de groep tot actieve deelname",
memoTitle: "De docent stimuleert de groep tot actieve deelname",
questionId: 15,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
}
],
[
{
memo: null,
grade: null,
description: "De docent voegt inhoudelijk iets toe aan het studiemateriaal",
memoTitle: "De docent voegt inhoudelijk iets toe aan het studiemateriaal",
questionId: 16,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "De docent is praktijkgericht",
memoTitle: "De docent is praktijkgericht",
questionId: 17,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "Totaal oordeel over de docent",
memoTitle: "Totaal oordeel over de docent",
questionId: 18,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
}
]
]
},
{
memo: null,
categoryId: 7,
title: "Opbouw programma en studiemateriaal",
hasMemo: true,
memoIsMandatory: false,
memoTitle: "Opbouw programma en studiemateriaal Opmerkingen",
questions: [
[
{
memo: null,
grade: null,
description: "Het studieprogramma is duidelijk opgebouwd",
memoTitle: "Het studieprogramma is duidelijk opgebouwd",
questionId: 54,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
},
{
memo: null,
grade: null,
description: "Het studiemateriaal is compleet, goed leesbaar en praktijkgericht",
memoTitle: "Het studiemateriaal is compleet, goed leesbaar en praktijkgericht",
questionId: 55,
hasGrade: true,
hasMemo: false,
showOnlyMemo: false
}
]
]
}
]

在我们的例子中,它是 questionId 55 的元素。有没有办法实现这一点?

4

1 回答 1

2

foreach绑定中,您可以使用特殊$index变量来引用您在循环中的位置。然后检查很简单。

<!-- ko if: $parent.$parent.hasMemo() && $index() + 1 === $parent.length -->
<article>
    <header data-bind="text: $parent.$parent.memoTitle"></header>
    <textarea></textarea>
</article>
<!-- /ko -->

请注意,当您在绑定中使用复杂的 JS 表达式(而不是单个 observable 或变量的名称)时,您需要显式地打开 observables。$index()因此和中的括号$parent.$parent.hasMemo()

于 2013-08-01T06:52:29.417 回答