【问题标题】:looping on a count in jsRender not working在jsRender中循环计数不起作用
【发布时间】:2020-03-02 19:29:00
【问题描述】:

我有以下几点:

 {{for Basket.Items itemVar="~product"}}
    {{for ~product.Qty}}
       ......
    {{/for}}
  {{/for}}

其中 ~product.Qty = 2。它只经过一次循环。如何使内部 for 循环达到 Qty 等于的次数?

所以我可能有 3 个篮子项目,其中 1 个项目我可能有 2 个,所以我希望我的模板两次显示数量为 2 的项目

【问题讨论】:

    标签: jsrender


    【解决方案1】:

    对于interate over a range of numbers,您需要将~product.Qty 作为{{for}} 的参数而不是{{for ~product.Qty}} 中的参数,而是命名为end 属性:{{for end=~product.Qty}}

    例如:

    {{for Basket.Items itemVar="~product"}}
      {{for end=Qty||1}}
         <div>{{:#index}} {{:~product.Name}}</div>
      {{/for}}
    {{/for}}
    

    {{for Basket.Items itemVar="~product"}}
      {{for end=~product.Qty||1 itemVar="~item"}}
         <div>{{: ~item}} {{: ~product.Name}}</div>
      {{/for}}
    {{/for}}
    

    与:

    data = {Basket: { 
        Items: [
            {Name: "A", Qty: 2},
            {Name: "B", Qty: 3},
            {Name: "C"},
            {Name: "D", Qty: 1}
        ]
    }};
    

    【讨论】:

    • 这对我不起作用。从您的数据示例中,我想在屏幕上看到名称“A”两次,因为数量为 2,名称“B”3 次,“C”一次,“D”一次。我仍然只看到我的项目一次。
    • 如果我使用你的第一个例子,在内部的末尾如果我这样做{{:#index}} 它看起来好像它已经循环了数量,但我只看到一个项目,即名称“A " 一次而不是两次,但我都出现在屏幕上 For #index in nested block use #getIndex()
    • @bilpor 一切如你所愿 - jsfiddle example
    • 我在 jsFiddle 中点击了运行按钮,没有任何反应
    • 点击“渲染模板”按钮!!它完美地工作。谢谢科德扳手。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多