How to Access Repeated Group Index inside Calc in that repeated group

bggann

Active Member
I have a repeated group that has various calculated fields in it.

I need to have one of those calculated fields only be calculated on the 1st repeated group. For subsequent repeated group, the value of that calculated field would be 0.

This would be easy if I can access the repeated group index from inside a calc field PHP.
if ( repeated_group_index = 0 ) { return 0; }
else { return calculated value;}

I tried using the placeholder {i} which can be used in the intro/outro for a repeated group to show the index - but inside the calc field, that placeholder is not recognized.

So - what can I do to access the repeated group index inside a repeated group calc field.
 
I think $repeatCounter should be in scope. At least in the main _getV() method.

Unfortunately, the calc element gets a bit funky, and there isn't always a $repeatCounter when it gets eval'ed. Like in AJAX calc'ing. So I just added that:

https://github.com/Fabrik/fabrik/commit/c092538b9fe3d5e3057fdf31e9531124b9a056e2

The reason being, in two places (AJAX calc and preFormatFormJoins) there isn't repeated data. The calc is being called in a single row context, where only one repeat's worth of data is present. And when repeat data is present we always handle it so if the calc uses a placeholder from it's own repeat group, we always make sure it has "this" repeat instance. Placeholders don't grok the concept of repeats, so if a calc is in a repeat and uses a placeholder for that group, the assumption is it just wants the value for "this" instance.

Anyway ... give it a go. With that commit, $repeatCounter should be there in most cases. The only exception would be if you have "Calc on save only" set to No, and are merging repeat data in the list. Which is when preFormatFormJoins() is called, which is what allows calcs to work on individual rows before the data is merge in a list.

-- hugh
 
@bggann - can you show your completed code after you try this? Would be great for the calc wiki also.
Yeah - I will - if I get it working. Right now I have another problem that is taking priority and feeds this calculation. I'm about to post on that one.
 
I'm bumping this for myself - I never got around to doing it, but I need to. I want to post the code when I'm done.
 
Worked like a charm - and quite simple.

The key is to realize that the $repeatCounter starts at 0 for the first repeat group.

So - the code is simple.
For me - I needed to add in a value to a total on the first repeat only.
if ( $repeatCounter == 0 ) {
$total += (float) $thingToAdd;
}

So the total was incremented by $thingToAdd on the first instance of a repeated group, and none of the other.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top