Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8139955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:00:01+00:00 2026-06-06T12:00:01+00:00

How do i count the items in a each jquery template and the apply

  • 0

How do i count the items in a each jquery template and the apply a if to that variable

something like

<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>

This is the javascript that loads the JSON and interacts with the jquery.TMPL

 var ip = window.location.hash;


    var request = $.ajax({
        type: 'GET',
        url: 'ajax/dashboard/widgets/inventorySummary.xsp?ip='+ip.substring(1),
        dataType: 'json'
    });

    request.done(function(data){
        $("#inventorySummaryTmpl").tmpl(data).appendTo("#inventorySummaryContent");
    });

    request.fail(function(jqXHR, textStatus, errorThrown) {
        console.log(errorThrown);
        console.log(textStatus)
    });

And the JSON

{

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

}

if someone has any ideeas.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:00:04+00:00Added an answer on June 6, 2026 at 12:00 pm

    You were pretty close. The variable in the each statement needs to be the full collection you are looping through. You can also pass custom variable names for the index and value of each item in the collection. Based on your code, the following should work, but if you posted the structure of your collection, we’d be able to ensure that it does

    <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>
    

    jQuery Docs: http://api.jquery.com/template-tag-each/

    UPDATE

    I’ve updated my solution now that I see what you’re after. If you want to start a new ul (or tr) after every three (or six, or whatever) elements in the response array, check the index of the current element, not the length of the full response array.

    <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>
    

    DEMO: http://jsfiddle.net/jackwanders/ZzddF/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using jquery &jquery UI. $.each(data, function(count,item) { showItem(item); // Dont continue without showItem
I'm using a DataPager control, I want to change count items on each page.
When I have a listview with like 46 items, where each item has 4
Having : var Difference: DWORD // difference shows in milliseconds // List.Items.Count can be
Is this the correct/best SPARQL query to get the count of items in rdf:list:
I have the following code: NSLog(@items: %d, [items count]); NSLog(@allObjects: %d, [self.allObjects count]); [self.allObjects
I am trying to get the count of the items as I'm applying a
I'm trying to count the number of purchases and 'up'/'down' votes for all items
I wrote this function to get the unread count of google reader items. function
When I attempt to count the number of items in a group I get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.