Retrieve number of elements in repeater

repeater-widget

#1

Hi all,

I’m using a repeater to display a table of content. Above, I use filter buttons to filter out only rows with specific properties. However, I’m struggling to display the number of elements matching each rule next to the filter buttons.

What I’ve tried is setting up global variables for the different properties and incrementing them in the load function of the repeater, which works for the first load, but then keeps incrementing each time the table gets sorted/filtered.

Any help is appreciated! Thanks.


#2

There’s a built in function to return the number of displayed items in a repeater - look for visibleItemCount in the function builder.


#3

Thanks. However, that returns the overall number of items. What I’m looking for is to be able to return the number of items with a certain parameter.

For instance, if I have this table:

  • John, Boy
  • Bob, Boy
  • Alice, Girl
  • Jennifer, Girl

I would like to return the number of boys and the number of girls in two separate fields.

Does that make sense? Is there a way to achieve that?


#4

I think you may be able to write a boolean test using the values here and the visibleItemCount method.

Something like:

[[Widget.visibleItemCount==‘Boy’]]

But my expression knowledge is fairly limited.


#5

There is not a built-in way to count items per filter or rule in an “ad hoc” way–at least that I’m aware of…

You could perhaps build in this functionality by adding some conditional cases in your OnItemLoad event. For instance, in your example, your repeater dataset would have a column for Name and a column for Gender. You could have conditional cases with “IF [[Item.Gender]] contains “Boy” set value of CountBoy to [[CountBoy + 1]]” and “IF [[Item.Gender]] contains “Girl” set value of CountGril to [[CountGirl + 1]]” --where CountBoy is a global variable (or text on a hidden widget if you don’t like too many global variables.) Of course, you’d want to initialize these variables to zero before counting your boys and girls, so preface those cases with a conditional case like “IF [[Item.isFirst]] equals “true” set CountBoy to 0”.

I haven’t tried this out, but it should work with filtered repeaters.

@davegoodman, regarding:

I believe this would always evaluate to “false” because the .visibleItemCount property is a numeric value.


#6

Thanks. This is what I had initially tried. The problem is that the OnItemLoad function is called whenever the repeater is refreshed (filtered, sorted, etc). This means that these values keep incrementing.

I could set another boolean variable to capture only the first load, but was wondering whether there was a more straightforward way to achieve that.


#7

Hi, i think you need a controller for it.

For example, you can insert a hidden widget or global variable name “RunCount”, and set this value to 0. Then add a condition for your repeater’s OnItemLoad like:

  • IF RunCount equals 0 --> Run the count calculator
  • ELSE IF RunCount equals 1 --> Don’t run the count calculator

Every time you use filter or sort, just set RunCount value to 1 before the filter or sort command. And set RunCount value to 0 when you want to count.


#8

Yes, that is why your first case in OnItemLoad should initialize the variables to zero for the first item, then increment as needed (which should work even for that first item.) If you do this, then you don’t need any other variables or “controllers.”