Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计数项目jquery模板_Jquery_Templates_Count_Compare - Fatal编程技术网

计数项目jquery模板

计数项目jquery模板,jquery,templates,count,compare,Jquery,Templates,Count,Compare,如何计算每个jquery模板中的项目数,并将if应用于该变量 差不多 <script id="inventorySummaryTmpl" type="text/x-jquery-tmpl"> <tr> {{each response}} {{if response.length === 2 }} </tr><tr> {{/if}} <td><img src='${icon}'>

如何计算每个jquery模板中的项目数,并将if应用于该变量

差不多

<script id="inventorySummaryTmpl" type="text/x-jquery-tmpl"> 
<tr>
{{each response}}
    {{if response.length === 2 }}
        </tr><tr>
    {{/if}}
        <td><img src='${icon}'> ${title} </td>
{{/each}}

</tr>
还有JSON

{

"response": [
    {
        "icon": "workstation",
        "nbItems": "38",
        "title": "Workstations"
    },
    {
        "icon": "server",
        "nbItems": "2",
        "title": "Servers"
    }
]
}


如果有人有任何想法。

你很接近。
每个
语句中的变量必须是循环所经过的完整集合。还可以为集合中每个项的索引和值传递自定义变量名。根据您的代码,以下内容应该有效,但如果您发布了收藏的结构,我们将能够确保它有效

<script id="inventorySummaryTmpl" type="text/x-jquery-tmpl"> 
<tr>
    {{each(index,resp) responses}}
        {{if resp.length === 2 }}
            </tr><tr>
        {{/if}}
        <td><img src='${resp.icon}'> ${resp.title} </td>
    {{/each}}
</tr>

演示

我将测试它并让每个人都知道。大约2-3个小时后,我更新了主要帖子,我会让你知道它不起作用。。。你有完整的信息,所以你可以测试它。也许你可以通过解释你希望最终的HTML输出是什么样子来帮助你。我很难理解你想让你的模板做什么。我想在第6个元素之后添加一个BR(可以这么说),这里是一个从中得出的结论,现在让我们说,我需要在第3个元素之后添加一个break
<script id="inventorySummaryTmpl" type="text/x-jquery-tmpl"> 
<tr>
    {{each(index,resp) responses}}
        {{if resp.length === 2 }}
            </tr><tr>
        {{/if}}
        <td><img src='${resp.icon}'> ${resp.title} </td>
    {{/each}}
</tr>
<script id="inventorySummaryTmpl" type="text/x-jquery-tmpl"> 
  <ul>
    {{each(i,el) response}}
      {{if (i > 0 && i % 3 === 0) }}
        </ul><ul>
      {{/if}}
      <li class="inventoryWrap">
        <img src='blueprint/images/wrap/icons/${icon}.png' class="inventoryPic"> 
        <span class="inventoryCount">${el.nbItems}</span> 
        <span class="inventoryText">${el.title}</span>
      </li>
    {{/each}}
  </ul>
</script>