gpt4 book ai didi

javascript - 迭代 meteorjs 中嵌套的每个语句(按类别分组)

转载 作者:行者123 更新时间:2023-11-30 16:27:21 25 4
gpt4 key购买 nike

我有一个产品列表,每个产品都有一个与之关联的类别。我正在尝试列出按类似于 this example 的类别分组的产品但我无法让它工作。它只显示一个唯一的类别列表而不是产品

我有以下 helper :

Template.supplierPage.helpers({
getCategories: function(){
var categories = Products.find({supplierId: this._id},
{sort:{category: 1}, fields: {category: 1}}).fetch();
return _.uniq( categories, true, function (product){
return product.category;
});
},
products: function(category) {
return Products.find({category: category, supplierId: this._id});
}
});

以及以下模板:

<template name="supplierPage">
<div class="ui dividing header">
<h3 class="ui header">My Products</h3>
</div>
<div>
{{> productCreate}}

{{#each getCategories}}
<h1>{{category}}</h1>
{{#each products.category}}
{{> productItem}}
{{/each}}
{{/each}}
</div>

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

你几乎做对了。您只是没有以正确的方式向 products 助手提供参数。这应该有效:

<template name="supplierPage">
<div class="ui dividing header">
<h3 class="ui header">My Products</h3>
</div>
<div>
{{> productCreate}}

{{#each getCategories}}
<h1>{{category}}</h1>
{{#each products category ../_id}}
{{> productItem}}
{{/each}}
{{/each}}
</div>

您还需要修复在 products 帮助程序中对 this 的使用,因为它会在使用 #each 时发生变化。最简单的方法是将其作为附加参数提供,如上所示:

products: function(category, supplier) {
return Products.find({category: category, supplierId: supplier});
}

关于javascript - 迭代 meteorjs 中嵌套的每个语句(按类别分组),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33906781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com