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

Radio button in repeating section - is this a bug? please help with this trivial scenario

1585496197

Edited 1585496665
admiralnlson
Sheet Author
I have the following code:                  < div   class = "fs-skill" > < label   class = "fs-skill-label" >< input   type = 'radio'   name = 'attr_selected_skill'   value = 'bureaucracy'   />< span   data-i18n = "bureaucracy" > Bureaucracy </ span ></ label >                      < input   type = "number"   name = "attr_bureaucracy"   type = "number"   min = 0   value = 0   />                  </ div >                  < div   class = "fs-skill" >                      < label   class = "fs-skill-label" >< input   type = 'radio'   name = 'attr_selected_skill'   value = 'investigation'   />< span   data-i18n = "investigation" > Investigation </ span > </ label >                      < input   type = "number"   name = "attr_investigation"   min = 0   value = 0   />                  </ div > [...]                  < div   class = "fs-col" >                      < fieldset   class = "repeating_creativeskills" >                          < div   class = "fs-skill" >                              < input   type = 'radio'   name = 'attr_selected_skill'   value = 'creativeskill'   />< input   class = "fs-skill-label"   name = "attr_creativeskill_name"   type = "text"   />                              < input   name = "attr_creativeskill_value"   type = "number"   min = 0   value = 0   />                          </ div >                      </ fieldset >                  </ div > The name of the radio button (attr_selected_skill) gets replaced with things like  attr_-M3asvXbMpWATUUiVTNL_repeating_creativeskills_selected_skill Its value on the other hand (creativeskill) is left unchanged. I was really expecting the opposite would happen (name left unchanged & value replaced). Is this a bug? What's the point of it working like this? Regardless of it being a bug or not, my objective is to ticking the radio button affect the value of a fixed attribute ("attr_selected_skill" in this case). How can I do that with the current roll20 version?
1585496522

Edited 1585496781
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
This is the point of repeating sections. Each row in the repeating section has a row id that is generated randomly. Every attribute in the repeating section is recreated for each row with the row id and the repeating section name prepended to it, so that a given row's version of each attribute is unique on the sheet. All together, a repeating attributes name for purposes of referencing it in macros/roll buttons is repeating_sectionName_ID_attribute_name  If it didn't do this, you wouldn't be able to call selected_skill from more than one row. EDIT: I take it you were trying to create a dynamic list of skills that players could add to and select which one was being used at a given time? This is still possible, it just takes a little more work and some sheetworkers. EDIT 2: missed your final question on my first read through. I'll post some sample code in a new post
1585497685

Edited 1585498999
admiralnlson
Sheet Author
> EDIT: I take it you were trying to create a dynamic list of skills that players could add to and select which one was being used at a given time? This is > still possible, it just takes a little more work and some sheetworkers. Kind of. I simply want to allow players to "select" a skill (normal or repeated ones). It's true that the current way repeating radio buttons are managed, it allows to have multiple radio buttons of the same type (i.e. with the same name) within a single entry of the repeating section, which.. actually.. is a use case I have somewhere else in my sheet. However, I need to make the opposite behavior (name left as-is, but value updated with the rowid) happen in this particular case. I suppose listening to "change:repeating_" events and updating the "attr_selected_skill" myself will work indeed. I'll try that. Thanks. Having a way to trigger this "opposite" behaviour as an option on radio buttons would be useful though. A potential improvement request to open.
1585498718

Edited 1585498919
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Here's my prototype code: <input name='attr_selected_skill' value='Add Skills below to select them'> <span>| value:</span><input type='number' name='attr_selected_value' value='0'> <fieldset class='repeating_creativeskills'> <input type='checkbox' name='attr_is_selected' value='1'> <input type='text' name='attr_name' value=''> <input type='number' name='attr_value' value='0'> </fieldset> <script type='text/worker'> on('change:repeating_creativeskills',(event)=>{//you'll also want to add handling for when one of these rows is removed to update the selected skill if that was the row that was removed. if(event.sourceType ==='worker'){ return;//prevents endless cascades of this sheetworker triggering itself } let row = event.triggerName; getSectionIDs('repeating_creativeskills', (itemIDArr) => { //assemble a list of all the custom skills let attributesToGet = _.reduce(itemIDArr,(memo,id)=>{ _.each(['is_selected','name','value'],(attributeName)=>{ memo.push(`repeating_creativeskills_${id}_${attributeName}`); }); return memo; },['selected_skill','selected_value']); //use that assembled list to get the attribute values for all the attributes in each row, as well as the non repeating attributes getAttrs(attributesToGet,(attributes)=>{ const setObj ={};//we'll use this object to store what attributes we want to change, and what we want to change them to if(/is_selected/.test(event.sourceAttribute)){//change which skill is selected if(attributes[`${event.triggerName}_is_selected`]*1===1){ setObj.selected_skill = attributes[`${event.triggerName}_name`]; setObj.selected_value = attributes[`${event.triggerName}_value`]; }else{ setObj.selected_skill = 'Select a skill below'; setObj.selected_value = 0; } _.each(attributes,(value,key)=>{ if(/is_selected/.test(key) && key!==event.sourceAttribute && value*1===1){ setObj[key]=0; } }); }else if(attributes[`${event.triggerName}_is_selected`]*1===1){//update the values for the currently selected skill setObj.selected_skill = attributes[`${event.triggerName}_name`]; setObj.selected_value = attributes[`${event.triggerName}_value`]; } setAttrs(setObj,{silent:true});//I use silent true here as a second precaution against causing an infinitely cascading loop of the sheetworker triggering itself. }); }); }); </script> EDIT: Fixed some typos
1585503493

Edited 1585514314
admiralnlson
Sheet Author
Thanks for the code snippet. EDIT: there's a typoe in  if(event.sourceType ==='worker'){  . Should be:  if(event.sourceType ==='sheetworker'){ . This makes the "silent" part unnecessary. Anyway, that's a ton of code to make a simple radio button work as many might expect radio buttons in repeating sections to work in the first place though. I still feel roll20 will need to allow for both scenarios for radio buttons in repeating sections at some point: radio button has a scope which is limited to the repeating section item => add the rowid to the name, leave the value unchanged (kept as default for backward compatibility, even though 2. would be a more natural default behaviour imho) radio button has a scope which goes beyond the repeating section item => leave the name unchanged, add the rowid to the value (e.g. if the dev has added a # prefix to it) I think I'll revise my sheet design to remove the need for radio buttons altogether, even if it means the sheet is less functional in the end. I don't like jumping through complex work-arounds like this to achieve what should be achievable simply.
1585507548

Edited 1585508103
admiralnlson
Sheet Author
Actually, never mind the above. It seems radio buttons are the only ones which have their names changed. I added a checkbox in the repeating section and its name is not changed by roll20: <fieldset class="repeating_creativeskills"> <div class="fs-skill"> <input type='radio' name='attr_selected_skill' value='attr_creativeskill_name' /> <input type='checkbox' name='attr_selected_skill' value='attr_creativeskill_name' /> <input class="fs-skill-label" name="attr_creativeskill_name" type="text" /> <input name="attr_creativeskill_value" type="number" min=0 value=0 /> </div> </fieldset> leads to (when inspecting the resulting HTML): <input type="radio" name="attr_-M3b_qcXunIPD7t35Zq6_repeating_creativeskills_selected_skill" value="attr_creativeskill_name" data-attrname="selected_skill"> <input type="checkbox" name="attr_selected_skill" value="attr_creativeskill_name"> <input class="sheet-fs-skill-label" name="attr_creativeskill_name" type="text"> <input name="attr_creativeskill_value" type="number" min="0" value="0"> This makes no sense to me. Why is the radio behaving differently from other input types?
1585509142

Edited 1585509774
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
There's something funky with your code. Every attribute should be renamed to include the row id. As for the expected behavior, I think that for most people, the current behavior is the expected behavior if you read the wiki on building character sheets and specifically the repeating sections bit.
1585509611

Edited 1585510429
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Hmm, looks like that is a bug. The row id doesn't get shown for checkboxes in the inspector, but it does still exist as a repeating attribute and doesn't exist as a non repeating attribute. Gonna submit that as a bug   Submitted this as a bug . EDIT: scratch that, radios are the only ones that get the row id appended to their attribute name in the parsed html. All repeating attributes still function the same though as everything takes it from the data-reprowid of the repitem div they are a part of.
1585515762

Edited 1585515836
admiralnlson
Sheet Author
Because radio buttons in repeating sections are buggy, I've replaced them with action buttons: <fieldset class="repeating_creativeskills"> <div class="fs-skill"> <button type="action" name="act_creativeskill-selectskill"></button> <input class="fs-skill-label" name="attr_creativeskill_name" type="text" /> <input name="attr_creativeskill_value" type="number" min=0 value=0 /> </div> </fieldset> on("clicked:repeating_creativeskills:creativeskill-selectskill", function (eventInfo) { if (eventInfo.sourceAttribute.endsWith("-selectskill")) { // roll20 communicates a sourceAttribute with caps in it, but stores it in lowercase, so for later callbacks to work.... let itemRoot = eventInfo.sourceAttribute.replace("-selectskill","").toLowerCase(); getAttrs([ itemRoot + "_name", itemRoot + "_value" ], function (values) { setAttrs({ selected_skill: Object.values(values)[0], selected_skill_i18n: Object.values(values)[0], selected_skill_repeatroot: itemRoot, selected_skill_value: Object.values(values)[1] }); }); } }); on("change:repeating_creativeskills", function (eventInfo) { getAttrs([ "selected_skill_repeatroot" ], function (values) { if (eventInfo.sourceAttribute.endsWith("_name")) { if (eventInfo.sourceAttribute === values.selected_skill_repeatroot + "_name") { getAttrs([ eventInfo.sourceAttribute ], function (values2) { setAttrs({ selected_skill: Object.values(values2)[0], selected_skill_i18n: Object.values(values2)[0] }); }); } } else if (eventInfo.sourceAttribute.endsWith("_value")) { if (eventInfo.sourceAttribute === values.selected_skill_repeatroot + "_value") { getAttrs([ eventInfo.sourceAttribute ], function (values2) { setAttrs({ selected_skill_value: Object.values(values2)[0] }); }); } } }); }); It works now.