underscore icon indicating copy to clipboard operation
underscore copied to clipboard

How do you display total number of records in a template?

Open lschneiderman opened this issue 7 years ago • 2 comments

I'm having trouble getting any answers on underscore.js on stackoverflow, so I'm just going to ask here. How do you display the total number of records in a template? In this example, how would I display totalNum?

lschneiderman avatar Jun 10 '19 18:06 lschneiderman

shouldn't it be

_.each(items,function(item,key,items) { %> <%- items.length %> of addresses in this search <% });

spattanaik75 avatar Jul 03 '19 14:07 spattanaik75

For future reference, if your items looks like this:

[{id: 100}, {id: 101}]

Then you can render something for each item and then display the total number of them at the end like this:

<% _.each(items, function(item, index, list) { %>
    <!-- print whatever you want about each `item` here -->
<% }); %>
Total number of items: <%= items.length %>

If you want to tally during iteration to show something like “item 5 of 10”, this will work:

<% _.each(items, function(item, index, list) { %>
    This is item <%= index %> of <%= list.length %>.
<% }); %>

@lschneiderman @spattanaik75

jgonggrijp avatar Apr 07 '20 19:04 jgonggrijp