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

Populating stati Fields from Repeating Section without Page Refresh

1713461717

Edited 1713462054
The title is somewhat vague, and I apologize for that! Once again, as a disclaimer, I am highly inexperienced, and any code I provide is likely to be very inefficient. So I'm working with a test sheet for another sheet I'm working on. The intention was to have an Inventory page with a Repeating Section where the players add weapons they acquire.  They also have a Combat Section with 3 static weapon slots, representing the Weapons they have readily accessable. So I put together a sheet worker ( I cannot express enough how much of this is just cannabalized from other Sheet Workers I've found, I'm still far from fluent in JS, let alone JS as it's used in Sheet Workers). It detects a change in a Dropdown (options: Slot 1, Slot 2, Slot 3), that when selected will fill in the corresponding fields in the Combat section. The issue I have run into is that it does update the corresponding fields, but requires the page to be refreshed, or for the Character Sheet to be closed and opened to reflect the changes. For the life of me I can't figure out what I'm missing to have it update in real time. Here is my html: <div class="combat"> <div class="weapon_stats" id="weapon_slot1"> <h3>Weapon Slot 1</h3> Weapon Name: <input type="text" name="attr_weapon1_name" readonly style="background: transparent; border: none;"> Damage Type: <input type="text" name="attr_weapon1_damage_type" readonly style="background: transparent; border: none;"> Damage: <input type="text" name="attr_weapon1_damage" readonly style="background: transparent; border: none;"> </div> <div class="weapon_stats" id="weapon_slot2"> <h3>Weapon Slot 2</h3> Weapon Name: <input type="text" name="attr_weapon2_name" readonly style="background: transparent; border: none;"> Damage Type: <input type="text" name="attr_weapon2_damage_type" readonly style="background: transparent; border: none;"> Damage: <input type="text" name="attr_weapon2_damage" readonly style="background: transparent; border: none;"> </div> <div class="weapon_stats" id="weapon_slot3"> <h3>Weapon Slot 3</h3> Weapon Name: <input type="text" name="attr_weapon3_name" readonly style="background: transparent; border: none;"> Damage Type: <input type="text" name="attr_weapon3_damage_type" readonly style="background: transparent; border: none;"> Damage: <input type="text" name="attr_weapon3_damage" readonly style="background: transparent; border: none;"> </div> </div> <div class="inventory"> <fieldset class="repeating_weapons"> <div class="weapon_entry"> <span class="stat-label">Weapon Name:</span> <input class="input" name="attr_weapon_name" type="text" placeholder="Enter Weapon Name"/> <span class="stat-label">Damage Type:</span> <input class="input" name="attr_damage_type" type="text" placeholder="Enter Damage Type"/> <span class="stat-label">Damage:</span> <input class="input" name="attr_damage" type="text" placeholder="Enter Damage"/> <span class="stat-label">Assign to Slot:</span> <select class="select" name="attr_weapon_slot"> <option value="">Select Slot</option> <option value="slot1">Slot 1</option> <option value="slot2">Slot 2</option> <option value="slot3">Slot 3</option> </select> </div> </fieldset> </div> and the Sheet Worker <script type="text/worker"> on('change:repeating_weapons:weapon_slot', function() { getSectionIDs('repeating_weapons', function(IDArray) { var fieldNames = IDArray.flatMap(id => [ `repeating_weapons_${id}_weapon_name`, `repeating_weapons_${id}_damage_type`, `repeating_weapons_${id}_damage`, `repeating_weapons_${id}_weapon_slot` ]); getAttrs(fieldNames, function(values) { var updates = { 'weapon1_name': '', 'weapon1_damage_type': '', 'weapon1_damage': '', 'weapon2_name': '', 'weapon2_damage_type': '', 'weapon2_damage': '', 'weapon3_name': '', 'weapon3_damage_type': '', 'weapon3_damage': '' }; IDArray.forEach(id => { var base = `repeating_weapons_${id}`; var slot = values[`${base}_weapon_slot`]; if (slot && slot.startsWith('slot')) { var slotNumber = slot.replace('slot', ''); updates[`weapon${slotNumber}_name`] = values[`${base}_weapon_name`]; updates[`weapon${slotNumber}_damage_type`] = values[`${base}_damage_type`]; updates[`weapon${slotNumber}_damage`] = values[`${base}_damage`]; } }); setAttrs(updates, function() { console.log("Combat section updated successfully based on weapon slots:", updates); }); }); }); }); </script> Again, it otherwise works as intended, but the update only occurs upon a page refresh, or by closing and opening the character sheet.
1713466231
GiGs
Pro
Sheet Author
API Scripter
Oneiris H. said: The issue I have run into is that it does update the corresponding fields, but requires the page to be refreshed, or for the Character Sheet to be closed and opened to reflect the changes. For the life of me I can't figure out what I'm missing to have it update in real time. I have seen this happen too, and to be honest, I'm as mystified as you as to what causes it - or what fixes it.  I'm kind of flailing in the dark here. One thing that is worth trying <input type="hidden" name="attr_weapon_slot" value=""> <select class="select" name="attr_weapon_slot"> <option value="" selected>Select Slot</option> <option value="slot1">Slot 1</option> <option value="slot2">Slot 2</option> <option value="slot3">Slot 3</option> </select> Note the addition of the hidden input and selected value for the dropdown (this is better then the undefined value it will have normally). Sometimes the value of a select is not properly detected unless its in a hidden input. That might not be relevant here but it's worth a try.
Thank you once again for coming to my rescue, GiGs! I applied the change as you suggested, but it still doesn't seem to be working without a page refresh, or closing the sheet. I have another Sheet Worker that you fixed up for me a while back that updates the total ENC of items in an Inventory that employs a Repeating Section <script type="text/worker"> on('change:repeating_inventory:item_enc change:repeating_inventory:item_amount remove:repeating_inventory', function() { getSectionIDs('repeating_inventory', function(IDArray) { var fieldNames = []; for (var i=0; i < IDArray.length; i++) { fieldNames.push('repeating_inventory_' + IDArray[i] + '_item_enc'); fieldNames.push('repeating_inventory_' + IDArray[i] + '_item_amount'); } getAttrs(fieldNames, function(values) { // since we are doing the whole repeating section, we need a variable to hold the values of each row var output = {}; // we also need to count the total weight as we go var total = 0; for (var i=0; i < IDArray.length; i++) { // we need to go through each row, and get the weight, cost, and amount // and calculate the totals for that row var weight = parseFloat(values['repeating_inventory_' + IDArray[i] + '_item_enc']) || 0; var amount = parseInt(values['repeating_inventory_' + IDArray[i] + '_item_amount']) || 0; // calculate the totals for THIS row var row_weight = weight * amount; // save those values for updating the sheet later output['repeating_inventory_' + IDArray[i]] = row_weight; // update the total weight total += row_weight; } // now we have looped through the entire set, we need to save the total weight output['current_enc'] = total; // now we can save the stats to the sheet. setAttrs(output); }); }); }); </script> It's updates happen as the changes are made to the ENC in the repeating sections. But for the life of me I can't figure out how to apply that to this new Sheet Worker. I'm sorry I'm not of more using in solving this, my knowledge is still so minimal in this that I'm more just a kid trying to cram puzzle pieces together until I find the right combination than an actual writer.
1713473463

Edited 1713485252
GiGs
Pro
Sheet Author
API Scripter
Oneiris H. said: Thank you once again for coming to my rescue, GiGs! It sounds like i didn't come to your rescue, this time! I do remember that other project though :) Don't beat yourself up. Your sheet  worker code is way more advanced than I'd expect from somene claiming their knowledge is minimal. Here's something else to try (with the proviso that it's just an attempt - I don't know what causes this for some workers): Have you tried with a completely new character? Sometimes sheets get corrupted, especially when designing sheets iteratively over time, and if you see behaviour that doesn't make sense, it's worth trying a different, new sheet.
GiGs... you're a mad genius 💟 Works perfectly now! I'll try to make a habit of starting fresh sheet after making major changes! and I thank you for your assessment, but again I can't stress how much of what I do is cannabilization, and generally riding off of the amazing work of other sheet authors like you! I hope I'll actually have a full understanding of sheet workers in the future Thank you once again!
1713485434

Edited 1713485675
GiGs
Pro
Sheet Author
API Scripter
Oneiris H. said: GiGs... you're a mad genius  Works perfectly now! I'll try to make a habit of starting fresh sheet after making major changes! Hehe, thank you. I've never seen anyone comment on this before, but I have seen it happen many times when working in the custom sheet sandbox (many, many times). But it's not predictable, there's no rhyme or reason to it, so I generally forget about it until I see a sheet acting weirdly, in a way I don't understand. Or sheet workers not acting when I know they should. That's when I try a new sheet. This doesn't happen with live sheets as far as i know, so it might be specific to the sandbox. The sandbox is still very valuable, but it's something to be wary of. I hope I'll actually have a full understanding of sheet workers in the future I don't know if you've seen this, but it might be worth a look: <a href="https://cybersphere.me/roll20-sheet-author-master-list/" rel="nofollow">https://cybersphere.me/roll20-sheet-author-master-list/</a> (especially the section on sheet workers - scroll down).
I don't know if you've seen this, but it might be worth a look:&nbsp; <a href="https://cybersphere.me/roll20-sheet-author-master-list/" rel="nofollow">https://cybersphere.me/roll20-sheet-author-master-list/</a> &nbsp;(especially the section on sheet workers - scroll down).\ I have! Your website has helped me immensely on my journey thus far! I think I won't have a more intimate understanding with it until I attempt to properly make a sheet worker from scratch without pulling from already made sheets. Nonetheless, your site is the reason I know what I do in the first place! I've said it before, and I'll say it again, I can't express enough how awesome you are for the resources you've provided with your site and for your continued support in the forums ^_^
1713497022
GiGs
Pro
Sheet Author
API Scripter
That's very sweet, the kind of pick-me-up that makes what I do worth doing. Thank you!