Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Repeatable Fields and Sheetworker... Having issues with field updating

1755551220
Falcon
Pro
Sheet Author
I have a weapon repeatable field, but having problems around using sheetworkers for repeatable fields because it makes it where the last row cannot be removed.  I know this has been handled before but someone should put it in wiki (I couldn't find it).  Here is the code: I am trying to put the melee score into the weapontotal attribute within the sheet without using @{melee} - using sheetworker.  Please let me know if this is the right direction and what I am doing wrong - right now it shows nothing in the field (blank). on("change:melee change:repeating_meleeweapon sheet:opened", function() {     getAttrs(['melee','repeating_meleeweapon_weapontotal'], function(values) {         const meleeValue = parseInt(values.melee);         getSectionIds("repeating_meleeweapon", function(idarray) {             const updateAttrs = {};             idarray.forEach(function(currentId) {                 updateAttrs[`repeating_meleeweapon_${currentId}_weapontotal`] = meleeValue;             });             setAttrs(updateAttrs);         });     }); });
1755552814
Falcon
Pro
Sheet Author
This works: on("change:repeating_meleeweapon change:melee sheet:opened", function() {      getAttrs(['repeating_meleeweapon_weapontotal','melee'], function(values){         setAttrs({repeating_meleeweapon_weapontotal: parseInt(values.melee) });     }); }); but then the last repeatable row cannot be deleted...
1755569484

Edited 1755569512
vÍnce
Pro
Sheet Author
Hi Falcon, I'm just a hack but, I believe you may need to include your getAttrs inside the getSectionIds.  Also not sure if  sheet:opened  is desired? See if this helps. on('change:melee change:repeating_meleeweapon', function () { getSectionIds('repeating_meleeweapon', function (idarray) { getAttrs(['melee', 'repeating_meleeweapon_weapontotal'], function (values) { const updateAttrs = {}; const meleeValue = parseInt(values.melee); idarray.forEach(function (currentId) { updateAttrs[`repeating_meleeweapon_${currentId}_weapontotal`] = meleeValue; }); setAttrs(updateAttrs); }); }); });
1755577033
Falcon
Pro
Sheet Author
Hmmm.  Didn't seem to work As you can see from the image that it still shows a 0 when a 5 should be shown.  I even started a new character to make sure it wasn't old code and even added a weapon but it still showing 0.  Here is the code of the repeating section it relates to         <fieldset class="repeating_meleeweapon"> ....           <input readonly class="sheet-center sheet-darkfringe-input" value="0" type="text" style="background:#00BCD4;color:black;font-weight:bolder;font-size:120%" name="attr_weapontotal">
1755577237
Falcon
Pro
Sheet Author
Sheetworker code for Melee if it helps: on('change:meleeranks change:strskillmod change:udm change:meleemanmod sheet:opened',function(){     getAttrs(['melee','meleeranks','strskillmod','udm','melredrawtotal','meleemanmod'], function(values){                     setAttrs({melee: parseInt(values.meleeranks) + parseInt(values.strskillmod)  });            setAttrs({melredrawtotal: parseInt(values.meleemanmod) - parseInt(values.udm)  });  }); });
1755577351

Edited 1755577423
Falcon
Pro
Sheet Author
Thanks Vince.  I just don't understand how the classic way works but this does not... But I hate the loop that is created so that the bottom row or last item of a repeatable section cannot be deleted when you perform this code.  If you have an alternative way - please let me know.   on("change:repeating_meleeweapon change:melee sheet:opened", function() {      getAttrs(['repeating_meleeweapon_weapontotal','melee'], function(values){         setAttrs({repeating_meleeweapon_weapontotal: parseInt(values.melee) });     }); });
1755584251
vÍnce
Pro
Sheet Author
Maybe there's a loop being created because the worker is triggered with "change:repeating_meleeweapon" ? Lets try including "silent: true" with the setter and removing "change:repeating_meleeweapon".  Also, "repeating_meleeweapon_weapontotal" doesn't need to be included in the getAttrs since it's value isn't being used for any calcs in this worker.  If you want to update all of your repeating_meleeweapon rows when the "melee" attribute changes (I'm assuming it's a non-repeating attribute...), you'll need to include getSectionIds with your worker otherwise the worker has no clue which rows to update.   on('change:melee', function () { getSectionIds('repeating_meleeweapon', function (idarray) { getAttrs(['melee'], function (values) { const updateAttrs = {}; const meleeValue = parseInt(values.melee); idarray.forEach(function (currentId) { updateAttrs[`repeating_meleeweapon_${currentId}_weapontotal`] = meleeValue; }); setAttrs(output, {silent: true}); }); }); });
1755586638
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Falcon said: This works: on("change:repeating_meleeweapon change:melee sheet:opened", function() {      getAttrs(['repeating_meleeweapon_weapontotal','melee'], function(values){         setAttrs({repeating_meleeweapon_weapontotal: parseInt(values.melee) });     }); }); but then the last repeatable row cannot be deleted... This won't work because when the change is from melee or sheet opened, the getAttrs will no longer be in the context of the repeating row. It's one of many reasons to use the full getSectionIDs method that Vince shows. As Vince suggests, the issue here is almost certainly an infinite loop caused by the change continually causing a change in the repeating row. The change listeners should almost always be as specific as possible. Also, note that Vince did not get the value of the repeating_meleeweapon_weapontotal attribute in his most recent suggestion. This is because it isn't used for any calculations in the code you've got and you only need to get attribute values if they might be used.
1755616196
Falcon
Pro
Sheet Author
It still is not working - showing the default value of 0 on the weapontotal with a new character sheet.  If you both think that it is formatted correctly (and constructed correctly) and should work.  then I will have to look at the rest of my code and find out what is stopping it from working... vÍnce said: Maybe there's a loop being created because the worker is triggered with "change:repeating_meleeweapon" ? Lets try including "silent: true" with the setter and removing "change:repeating_meleeweapon".  Also, "repeating_meleeweapon_weapontotal" doesn't need to be included in the getAttrs since it's value isn't being used for any calcs in this worker.  If you want to update all of your repeating_meleeweapon rows when the "melee" attribute changes (I'm assuming it's a non-repeating attribute...), you'll need to include getSectionIds with your worker otherwise the worker has no clue which rows to update.   on('change:melee', function () { getSectionIds('repeating_meleeweapon', function (idarray) { getAttrs(['melee'], function (values) { const updateAttrs = {}; const meleeValue = parseInt(values.melee); idarray.forEach(function (currentId) { updateAttrs[`repeating_meleeweapon_${currentId}_weapontotal`] = meleeValue; }); setAttrs(output, {silent: true}); }); }); });
1755618232
Falcon
Pro
Sheet Author
Vince / Scott, Appreciate the help.  I do not understand but somewhere or for some reason it won't update the fields even with a new character and a new row.  Therefore I am going to go back and use autocalc field which worked before and change my role parsing to look up melee and range instead of totalweapon.  I really appreciate the help on this.
1755621765

Edited 1755622853
vÍnce
Pro
Sheet Author
Don't throw in the towel just yet. ;-P  Part of the fun is solving the problems.  I did a little test and noticed that the worker was throwing an error in the console log (ReferenceError: getSectionIds not defined) I updated to the expected " getSectionIDs "  Checking the log is very important when developing. I also added "change:repeating_meleeweapon:name" although that might be something else in your repeating section.  I just want the worker to fire when you create a new weapon so that it can pull the "melee" value at that time. as well as when "melee" is changed.  You may want to trigger with other changes as well.  (ie other mods in the repeating section perhaps...)  Anyhow this worked in my test. Maybe you can use it as an example to get you past your problem. on('change:melee change:repeating_meleeweapon:name', function () { // when should this run? getSectionIDs('repeating_meleeweapon', function (idArray) { // grab repeating section IDs getAttrs(['melee'], function (values) { // grab any attribute names you need for calcs const updateAttrs = {}; // placeholder that will be used to execute all setters later const meleeValue = parseInt(values.melee); // grab value idArray.forEach(function (currentId) { // loops through all rows updateAttrs[`repeating_meleeweapon_${currentId}_weapontotal`] = meleeValue; // sets the value }); setAttrs(updateAttrs, {silent: true}); // using silent unless you need to trigger other workers? }); }); }); // simple HTML for testing <span>Melee:<input type="text" name="attr_melee" value="0" title="@{melee}" /></span> <fieldset class="repeating_meleeweapon"> <span>Name:<input type="text" name="attr_name" value="" title="@{name}" /></span> <span>Weapon Total:<input type="text" name="attr_weapontotal" value="0" readonly/></span> </fieldset> Updated: added comments.
1755624418
Falcon
Pro
Sheet Author
Ha!!  That worked Vince.  Yeah the weapon name is:  weaponname.  As soon as I changed that and added it.  It seemed to work.  There must have been some syntax error?
1755624912
Falcon
Pro
Sheet Author
Thanks for sticking with it.  I now can use this sheetworker code for other calculating fields in my repeatable sections