【问题标题】:Displaying name field for each array element inside a collection显示集合中每个数组元素的名称字段
【发布时间】:2018-01-16 16:03:18
【问题描述】:

我有一个集合(类别),每个文档都有一个“产品”字段,它是一个对象(产品)数组。每个产品都有一个“名称”和一个“起始成本”字段。我可以显示每个类别名称,但我不知道如何在每个类别中显示每个产品名称。

HTML:

<template name='categoriesList'>
    <ul class='category-grid'>
        {{ #each categories }}
            <li>{{ name }}</li>
            <!-- Display every product's name -->
        {{/each}}
    </ul>
</template>

JS:

Template.categoriesList.helpers({
    categories() {
        return Categories.find({});
    },
});

架构:

{
  "_id" : "test",
  "name" : "TestNAME",
  "createdAt" : ISODate("2017-08-08T18:03:09.508Z"),
  "products" : [
    { "name" : "testProduct", "startingCost" : 100 },
    { "name" : "test2", "startingCost" : 200 }
  ]
}

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    应该可以再做一次{{ #each }}

    <template name='categoriesList'>
        <ul class='category-grid'>
            {{ #each category in categories }}
                <li>{{ category.name }}</li>
                <!-- Display every product's name -->
                <ul>
                    {{ #each product in category.products }}
                    <li>{{ product.name }}: ${{ product.startingCost }}</li>
                    {{/each}}
                </ul>
            {{/each}}
        </ul>
    </template>
    

    您是否更喜欢 each productseach product in products 语法。嵌套时,我绝对更喜欢命名空间。

    【讨论】:

    • 是的!我想我在某个时候尝试过,但没有成功(我确定我有一些简单的拼写错误或拼写错误的名称)。但现在可以了。
    • 很高兴听到。想通过接受答案来赠送我一些虚假的互联网积分吗?
    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多