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

Reordering Repeating Section Contents

1658596177

Edited 1658596513
Initial poking around through forum history indicates that there hasn't been a practical  feature implemented that allows for a repeating section to be re-sorted programmatically based on one of its attributes. (Yes, yes, I see the scripts, but that seems too complex for such a commonly requested need. Also, the documentation for setSectionOrder comes with the kind of warning that screams, "do not use!") What I need is a simple Sheet Worker (NOT an API) script that will reorder a repeating section based on a given attribute value . i.e. Provide a list name, an attribute, and a sorting direction (ascending/descending), then apply this in alpha-numeric fashion to the list and update the view on the character sheet. I don't care about how this occurs on the back-end, just "please, sort this list." Does this already exist? If not, why not (Roll20)? It seems like a common feature for any list that actual humans (non-programmers) need to actually use regularly. My only current option is to manually delete everything in the list and re-add it every time I want to adjust the list's internal sort order. This is a critical need , IMO, for using tools like ChatMenus. My players are frustrated with un-sorted list contents. Thanks!
Pseudo code for what I'm looking for: on('change:repeating_sectionName:someAttribute', function(eventInfo) { do stuff...      sortSectionIDs (sectionName, someSectionAttribute, 1|0); });
P.S. At the very least, a repeating list should just re-order internally to match the order of items in the UI.
1658604176
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Setsectionorder is what you're looking for, and yes, it does have issues. For the ordering the list sheetworker idarray based on what the actual ui shows, I e got a function that does that in the k-scaffold (and the idarray provided by the k-scaffold are auto sorted I to ui order)
Thanks. Where can I find that sheetworker function?
1658614027

Edited 1658614184
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
You linked to the sheetworker documentation in your OP. Edit: if you mean the k-scaffold function, you can find details on the k-scaffold's documentation 
1658623952
GiGs
Pro
Sheet Author
API Scripter
While it can be done programmatically, as Scott says, it's also resonable to leave this to be done manually by the player. That's how I always do it :)
GiGs said: While it can be done programmatically, as Scott says, it's also resonable to leave this to be done manually by the player. That's how I always do it :) That's exactly the issue: regardless of how the player re-organizes the list, the sheetworker functions used to iterate through that list do NOT present the items in their display order.
P.S. I wouldn't even have noticed this issue if ChatMenus weren't so awesome  for speeding up play. Its a requirement in all my games now, simply because players and myself don't have to keep opening the character sheet over and over (nor have to go looking for buttons to click.) Roll20 is great for the coding hobbyist because its simple, extensible, and there's a lot of community-created stuff. It's not so good if/when basic/common problems require complex engineering work-arounds. (k-scaffold/Pug appear to be more than I want to bite off - I'd rather just deal with out-of-order list contents.)
1658772789
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Sorry, omonubi, I wasn't clear. It should be possible to steal the order section function from the k-scaffold nearly whole. I'll try to pull it out for you later today.
1658778295
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Here's the orderSection  function from the K-scaffold. It doesn't require any K-scaffold structures to use: /** * Orders a single ID array. * @param {[string]} repOrder - Array of IDs in the order they are in on the sheet. * @param {[string]} IDs - Array of IDs to be ordered. */ const orderSection = function(repOrder,IDs=[]){ IDs.sort((a,b)=>{ return repOrder.indexOf(a.toLowerCase()) - repOrder.indexOf(b.toLowerCase()); }); };
1658803928
GiGs
Pro
Sheet Author
API Scripter
omonubi said: GiGs said: While it can be done programmatically, as Scott says, it's also resonable to leave this to be done manually by the player. That's how I always do it :) That's exactly the issue: regardless of how the player re-organizes the list, the sheetworker functions used to iterate through that list do NOT present the items in their display order. When is this an issue? (I'm not being snarky, I'm genuinely curious.)
1658811933

Edited 1658812303
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
GiGs, they're talking about the order that the IDs are presented in when you do a getSectionIDs . The IDs from getSectionIDs are always in lexographic order (aka the order they were created), not the order that the user has put them in. You need a function like the orderSection above to properly sort the IDArray from getSectionIDs. Of course, that means you also have to grab the pseudo attribute _reporder_repeating_SECTION (replacing SECTION with your section's name) in order to get the order of the items that have been reordered. EDIT: Omonubi, here's an example of how you'd use the orderSection function: getSectionIDs('repeating_SECTION',(idArray)=>{ //Setup an array of attributes to get with the repeating ids const getArr = idArray.reduce((memo,id)=>{ memo.push(`repeating_SECTION_${id}_weight`,`repeating_SECTION_${id}_quantity`); return memo; },['_reporder_repeating_SECTION']); getAttrs(getArr,(attributes)=>{ //Parse the reporder into an array attributes._reporder_repeating_SECTION = JSON.parse(attributes._reporder_repeating_SECTION); //Order the section IDs so that they are in the correct order orderSection(attributes._reporder_repeating_SECTION,idArray); //Do things with the attributes and IDs as needed. }) });
1658822313
GiGs
Pro
Sheet Author
API Scripter
Scott C. said: GiGs, they're talking about the order that the IDs are presented in when you do a getSectionIDs . That's what I thought was being discussed. I'm just curious over when it would make a difference to have a sheet worker run in the original order, or the reordered order. It's still going to operate in every row in the section, exactly once, so what difference does the order make, and when does the order make a difference? I'm sure there is a situation where it matters that I havent realised - that's what I'm asking about. I guess if you want to show a number identifying each row, it would matter to use the visible order then.
1658845016
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
Yep, number for each row, or if you wanted to output a chat menu and have the buttons be ordered like the section is.
Scott C. said: Here's the orderSection  function from the K-scaffold. It doesn't require any K-scaffold structures to use: Awesome! I'll give it a try.
Scott C. said: Yep, number for each row, or if you wanted to output a chat menu and have the buttons be ordered like the section is. Exactly. My games almost exclusively use ChatMenu macros to output sheet buttons as links. For repeating sections, I have no control over the order and the script defaults to lexographic order. With 20+ items on the chatmenu is Roll20 chat, it can be a bear to find something if they aren't ordered more intuitively.
1658885107
GiGs
Pro
Sheet Author
API Scripter
Thanks, you two. I may be slow sometimes, but those purposes do make sense. :)