【问题标题】:Meteor template each loop, provide other template each X resultsMeteor模板每个循环,提供其他模板每个X结果
【发布时间】:2016-07-21 01:13:42
【问题描述】:

当meteor解析带有{{#each}}模板逻辑的集合时,是否可以只插入特定的数据?

示例:

HTML
    {{#each collection}}
                 <div>
                    <p>{{collectionparam1}} - {{collectionparam2}}</p>
                    {{#if **collection[only multiple of 2]**}}
                       <p>I show this {{collectionparam3}} only each 2 entries</p>
                    {{/if}}
                 </div>      
    {{/each}}

结果:

Billy - 24
Joe - 12
I show this banana only each 2 entries
Bob - 88
Maria - 5
I show this cherry only each 2 entries
Samantha - 102
Henry - 43
I show this apple only each 2 entries

如果可能,我必须在 {{#if}} 逻辑上添加什么?

感谢您的帮助。

【问题讨论】:

    标签: templates if-statement meteor collections each


    【解决方案1】:

    这类似于this question。试试这个:

    html

    {{#each collection}}
      <div>
        <p>{{collectionparam1}} - {{collectionparam2}}</p>
          {{#if shouldShow @index}}
            <p>I show this {{collectionparam3}} only each 2 entries</p>
          {{/if}}
      </div>
    {{/each}}
    

    js

    Template.myTemplate.helpers({
      shouldShow: function (index) {
        return (index + 1) % 2 === 0;
      }
    });
    

    【讨论】:

    • 完美就是这样。我只需要在 js 模板助手中更改 % 之后的数字,以便仅显示我收藏的每个 X 条目的值。
    • 是的,你也可以使用第二个参数——比如{{#if shouldShow @index 3}} 可以每隔第三个参数做一次。这样你就可以有一个助手并在多个地方使用它。
    猜你喜欢
    • 2014-11-06
    • 2011-11-13
    • 2015-06-06
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2014-08-05
    • 2014-06-24
    相关资源
    最近更新 更多