default value element repeat group

eurtnl

New Member
I’m creating a website where golfers can record their scores.

The setup
Form addScore has two groups:
  1. Details (elements: Name, Date, Course, Starting hole)
  2. Score (Hole, score)
The group Score is repeatable with Max repeat 18 en Min Repeat 18.
Now I have two questions:
  1. How can I pre-calculate the element Hole on all repeatable groups so that in the first group the value would be 1, in the second group the value would be 2 etc.
  2. But ideal would be that the player would fill in the starting hole (like 5) and than in the first group the value of element Hole would be 5, in the second group the value would be 6 etc. till hole 18 and then the next group would be 1 etc.
 
Ok, I got the first part working. The tricky thing was when to execute the js because the page doesn't render all repeat groups onload page. So i put the js in an mandatory element (onchange).
JavaScript:
requirejs(['fab/fabrik'], function SetDefaultValue() {
    var i = document.getElementById('tst_score___start_hole').getValue();
    document.getElementById("tst_score_2_repeat___hole__0").value=0 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__1").value=1 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__2").value=2 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__3").value=3 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__4").value=4 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__5").value=5 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__6").value=6 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__7").value=7 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__8").value=8 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__9").value=9 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__10").value=10 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__11").value=11 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__12").value=12 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__13").value=13 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__14").value=14 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__15").value=15 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__16").value=16 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__17").value=17 + parseFloat(i);
})

Now I would like some help with the second part:
There are a max of 18 holes. During a tournament it is possible a player starts his round on hole 6 (instead of hole 1). In my script it starts with 6 but after 18 it keeps counting till 24 and i need it to start allover after 18. So the hole numbers would be like
6
7
8
9
..
18
1
2
3
4
5
 
It should be possible with some modulo operation, something like (I didn't test, maybe you have to add 1)
(17 + parseFloat(i)) % 18
 
Thank you. Your suggestion did do something but not what I hoped. (I also tested it here and that seemed to work just fine). So my code looks like this:
JavaScript:
requirejs(['fab/fabrik'], function SetDefaultValue() {
    var i = document.getElementById('tst_score___start_hole').getValue();
    document.getElementById("tst_score_2_repeat___hole__0").value=0 + parseFloat(i);
    document.getElementById("tst_score_2_repeat___hole__1").value=1 + parseFloat(i) % 18;
    document.getElementById("tst_score_2_repeat___hole__2").value=2 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__3").value=3 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__4").value=4 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__5").value=5 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__6").value=6 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__7").value=7 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__8").value=8 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__9").value=9 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__10").value=10 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__11").value=11 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__12").value=12 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__13").value=13 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__14").value=14 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__15").value=15 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__16").value=16 + parseFloat(i)  % 18;
    document.getElementById("tst_score_2_repeat___hole__17").value=17 + parseFloat(i)  % 18;
})

When I select hole 18 as staring hole, it works as expected:
Screenshot 2020-11-08 at 00.21.56.png

But when I select hole 17 as starting hole, it doesn't work:
Screenshot 2020-11-08 at 00.22.15.png
 
Try
JavaScript:
document.getElementById("tst_score_2_repeat___hole__1").value= (1 + parseFloat(i)) % 18;
 
Thank you. I tried it with a loop and now it works correct :)
JavaScript:
requirejs(['fab/fabrik'], function SetDefaultValue() {
var holes = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17];
var starthole = document.getElementById('tst_score___start_hole').getValue();
var i;
for (i = 0; i < 18; i++) {
  document.getElementById("tst_score_2_repeat___hole__"+i).value = (((starthole-1) + holes[i]) % 18) + 1;
  }
});
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top