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

How do I get the row index for the current row in a repeating section

I have a button in a repeating section that looks like this <button type="roll" class="menr-button" value="!setattr --silent --name @{character_name} --repeating_wounds_$0_wound-desc|?{Description|New wound}">Edit description</button> It works great for the first row, but I need it to edit the current row. I need to replace the "$0" with the current row index. How do I get that?
You count, starting from $0 2nd one is $1 3rd is $2 and so on
I got that part, but the button is inside the repeating section, and I need it to address the row from which it was clicked. I need to figure out how to get the index for the current row.
1613185211

Edited 1613185275
GiGs
Pro
Sheet Author
API Scripter
If this was a normal macro (not an API script), you wouldnt need to use the index: @{wound-desc} for instance would automatically fill in the section name and row id automatically. Since its a script call, the easiest way (maybe the only way) is to have a sheet worker that fills it in automatically. You'd change the button to something like this: <button type="roll" class="menr-button" value="@{wound-button}">Edit description</button> <input type="hidden" name="attr_wound-button" value=""> And then a sheet worker like this: on('change:repeating_wounds, sheet:opened', () => {     getSectionIDs('repeating_wounds', idarray => {         const output = {};         idarray.forEach(id => {             output[`repeating_wounds_${id}_wound-button`] = `!setattr --silent --name @{character_name} --repeating_wounds_${id}}_wound-desc|?{Description|New wound}`;         });         setAttrs(output);     }); }); Then whenever you create a new row or make any change to the repeating section, that worker will calculate the proper values for the string and fill the wound-button attribute on each row, which the button will use when you click it.
1613185367
GiGs
Pro
Sheet Author
API Scripter
Honestly, I'm wondering why you have a button for this, and not just a text box in the repeating section that people enter the text directly.  They have to have the sheet open to add a new row, to create a wound entry, might as well just type it into a checkbox while they are doing it.
I had a variety of text boxes and number inputs to represent the severity, damage, and description, but I wanted to minimize player interaction with those stats and let them be handled through other macros and events, while still providing the ability to manually edit them if necessary.  A menu seemed like a good way to do this.
And thank you, yet again.
1613244400
GiGs
Pro
Sheet Author
API Scripter
You're welcome :)
I'm creating the rows using this button:  <button type="roll" class="add-wound r" title="Add wound" value="!setattr --name @{character_name} --silent --repeating_wounds_-CREATE_damage|?{Damage|0} --repeating_wounds_-CREATE_wound-desc|New wound"></button> How do I get it to put the new row id into the "wound-id "input? I'm thinking I need to add " --repeating_wounds_-CREATE_wound-id|???, but I don't know how I get the id that would replace the "???"
To clarify, I have this script: on('change:repeating_wounds, sheet:opened', () => { getSectionIDs('repeating_wounds', idarray => { const output = {}; idarray.forEach(id => { output[`repeating_wounds_${id}_wound-id`] = id; }); setAttrs(output); }); }); but it doesn't trigger when the new row is created. If I tab away or refresh, it triggers, but not when the row is first created.
1613383774
Oosh
Sheet Author
API Scripter
I don't write sheets, so take this with a grain of salt - I think you might want to remove the sheet:opened event. I don't think it's a continuous event, I think it's getting triggered by your alt-tabbing... but I could be wrong.
When the sheet is opened, it triggers and all the rows update. It is just when the row is first created that it doesn't trigger. I get the sense that the row isn't really registered until after something happens. I notice, for instance, that, if you create a row with the Add button, and then tab away, the row disappears, unless you have altered one of the items in the row. So I think i need either a way to manually enter the row id from ChatSetAttr, or some way to forcibly refresh the repeating section.
Is there any way to refresh the character sheet from the api, and thus trigger the sheet:opened  event?
1613407186
GiGs
Pro
Sheet Author
API Scripter
I dont know if this is the cause of your issue, but my original code had an error. This on('change:repeating_wounds, sheet:opened', () => { should be on('change:repeating_wounds sheet:opened', () => { (no comma there).
Thanks. Fixed that, but it's still not triggering the sheet worker. If I close the character and reopen it, it triggers fine. It's just the api call that is not triggering the worker. It creates the new row and assigns the values fine, but I can't figure out to get it assign the row id to "wound-id" automatically on creation. None of my macros to update items in the row will then work until the sheet is refreshed and the wound-id input updates. The only things I can think of are either to insert the rowid manually as part of the !setattr call (which I don't now how I'd get that id), or to somehow reset the sheet after creating the new row, which would then trigger the sheet:opened  event. Perusing the forums, I see a bunch of posts about api calls not triggering sheet workers and that having something to do with the source being 'API' instead of 'Player' but I don't see any solutions. 
1613409461
GiGs
Pro
Sheet Author
API Scripter
there have always been problems with getting sheet workers to run automatically with the API (its not natively supported and was a later feature added). chatSetAttrs normally does work, but as you say, when called from the API itself it might not. I don;t know a solution to this. The way to get a sheet worker to update when a new row is created, is to make sure you have an attribute with a default value somewhere in that repeating section. It can a hidden attribute. Something like <input type="hidden" name="attr_dummy" value="row created"> should work. The row itself does not really exist until it has an attribute with a value inside it.
I tried adding this and it still doesn't update. Nothing seems to trigger the worker other than opening the sheet
1613414051

Edited 1613414119
GiGs
Pro
Sheet Author
API Scripter
Can you post the full code for your repeating section? I just noticed this earlier post: Randall S. said: When the sheet is opened, it triggers and all the rows update. It is just when the row is first created that it doesn't trigger. I get the sense that the row isn't really registered until after something happens. I notice, for instance, that, if you create a row with the Add button, and then tab away, the row disappears, unless you have altered one of the items in the row. So I think i need either a way to manually enter the row id from ChatSetAttr, or some way to forcibly refresh the repeating section. This is normal behaviour. This is exactly how repeating sections work. Until you actually create or update something inside the repeating section, it doesnt really exist. The Add button doesnt actually save a new row, it just creates the fields for you, which you then need to enter something for the row to be saved. Having default values in an attribute usually solves that problem. That said, I don't understand why sheet:opened  seems to be working, but the change: entry isnt - if one works, both should work, which is why I need the html to test.
the html is: <fieldset class="repeating_wounds">     <div class="woundRow f"> <div class="wound-menu"> l <div class="menu-show"> <div class="wound-menu-option damage-option"> <button type="roll" class="menr-button" value="!setattr --silent --name @{character_name} --repeating_wounds_@{wound-id}_wound-desc|?{Description|@{wound-desc}}">Edit description</button> </div> <div class="wound-menu-option damage-option"> Damage <div class="wound-menu-option damage-show option-show"> <div class="wound-menu-option"> <button type="roll" class="menr-button" value="!setattr --silent --name @{character_name} --repeating_wounds_@{wound-id}_damage|?{Damage|@{damage}}">Edit damage</button> </div> <div class="wound-menu-option"> <button type="roll" class="menr-button">Heal</button> </div> <div class="wound-menu-option"> <button type="roll" class="menr-button" value="!setattr --silent --name @{character_name} --repeating_wounds_@{wound-id}_damage|0">Delete wound</button> </div> </div> </div> <div class="wound-menu-option bleed-option"> Bleed <div class="wound-menu-option bleed-show option-show"> <div class="wound-menu-option"> <button type="roll" class="menr-button">Edit bleed level</button> </div> <div class="wound-menu-option"> <button type="action" name="act_bandage" class="men-button">Bandage</button> </div> <div class="wound-menu-option"> <button type="action" name="act_close-wound" class="men-button">Close</button> </div> </div> </div> <div class="wound-menu-option poison-option"> Poison <div class="wound-menu-option poison-show option-show"> <div class="wound-menu-option"> <button type="roll" class="menr-button">Edit poison level</button> </div> <div class="wound-menu-option"> <button type="action" name="act_slow-poison" class="men-button">Slow</button> </div> <div class="wound-menu-option"> <button type="action" name="act_cure-poison" class="men-button">Cure</button> </div> </div> </div> </div> </div>         <input type="hidden" name="attr_wound-id">         <span class="damage-label">D:</span>         <input type="hidden" name="attr_damage"> <span name="attr_damage" class="damage-select"></span>         <div class="damage-cutout"> </div>         <div class="damage-overlay"> </div>         <input type="hidden" name="attr_severity" value="none"> <span name="attr_severity-desc" class="severity-select"></span>         <input type="hidden" name="attr_wound-desc" value="a"> <span name="attr_wound-desc" class="wound-desc"></span> <span name="attr_wound-id" class="wound-id"></span> <input type="hidden" name="attr_dummy" value="row created">     </div> </fieldset>
The CSS is: button[type="roll"].sheet-menr-button:before, button[type="action"].sheet-men-button:before{ content:""; } button[type="roll"].sheet-menr-button:focus, button[type="action"].sheet-men-button:focus{ outline:none; } button[type="roll"].sheet-menr-button, button[type="action"].sheet-men-button{ background:none; border:none; font-size:8px; padding:0; position:relative; top:-2px; color:white; } button[type="roll"].sheet-menr-button:active button[type="action"].sheet-men-button:active{ outline:none; } .sheet-menu-show{ display:block; margin-left:12px; position:relative; top:-12px; z-index:2; } .sheet-wound-menu{ font-family:"Pictos"; position:relative; top:-2px; margin-left:8px; margin-righ:-100px; } .sheet-wound-menu:not(:hover) > .sheet-menu-show{ display:none; } .sheet-wound-menu-option:hover{ background-color:red; } .sheet-wound-menu-option{ background-color:#000000; font-family:arial; font-size:8px; color:white; width:100px; height:12px; text-align:center; z-index:1; } .sheet-damage-option:not(:hover) > .sheet-damage-show, .sheet-bleed-option:not(:hover) > .sheet-bleed-show, .sheet-poison-option:not(:hover) > .sheet-poison-show{ display:none; } .sheet-option-show{ font-family:arial; font-size:8px; color:white; position:relative; top:-12px; left:99px; z-index:3; } button[type="roll"].sheet-wound-menu:active{     box-shadow:none;     position:relative;     top:-5.5px;     left:.5px; } button[type="roll"].sheet-wound-menu:focus{ outline:none; } button[type="roll"].sheet-wound-menu{ background:none; border:none; position:relative; top:-6px; } button[type="roll"].sheet-wound-menu:before{ font-family:pictos; font-size:12px; content:"l"; }
I put a span in to show whether the id populated. When I create the row, it looks like this: But when I close and reopen the character, it populates:
I went back to the api and unchecked " Trigger sheet workers when setting attributes" and then rechecked it and it seems to have fixed the issue.
1613429689
GiGs
Pro
Sheet Author
API Scripter
Thats good to hear :)