Drop Down Counter

Hello all,
I am trying to make a button that will create or delete a drop down counter and I have the counter code figured out but not the buttons idealy what I would like is to be able to drop some code into a tiddler that when viewed would put a drop down counter in place and if I need 3 i hit the + button 3 times and if i am done with that counter i can click the - sign and remove that counter. I dont know if that is possible to do but thought I would ask. I love this community and I love tiddlywiki!

Thrugar

Counter 1<br>
<$select tiddler="MyTiddler" field="selectedNumber">
    <$list filter="[range[0,100]]">
        <option><<currentTiddler>></option>
    </$list>
</$select>
  • Edited to wrap code in three back ticks by TW_Tones

I would love to help, but I dont understand the question.

  • What is a drop down counter and what does create or deleting it mean?
  • Also it seems rather than putting code into the tiddler you would be best displaying it on tiddlers via a tiddler with the $:/tags/ViewTemplate tag.
    • Within this tviewtemplate tiddler there would be a condition when to display the add counter, and when to display the counter code, and delete counter to be displayed on tiddlers.

TW_Tones.
The code i provided makes a counter you can click on it and choose a number between 0 and 100.
What I am looking to do is to be able to make multiple counters upon request. i.e if I wanted 3 counters that each counted to 100 I could press a “+ button” 3 times to make 3 counters. If I created say 5 counters I would like to delete the extra 2 I created by accident. or any other simple method of making more counts or removing them once created, if they are not needed.
I hope this explains it better I am sorry if I did not explain it well enought the 1st time. Thank you for your assitance it is very much appreciated.

Thrugar

If the counters are stored in fields on the current tiddler, each click of a + could simply create counter-1 counter-2 etc … fields. Then something on the view template can discover that the fields counter-* exist and for each provide a counter button set. including a button to delete the current counter-N field to remove it.

Alternativly one could use the suffix -counter for each field and use what comes before the suffix as the name of the counter eg visit-counter = the visit counter.

TW_Tones I am unfortunately not a very good coder, could you suggest code to do that?

thank you

I will see if I can share parts of the solution first. later if I have time the solution. are you happy adding the something-counter fields using edit mode?

As long as I can add it to any Tiddler that’s fine. Again thank you very much for your help

Thrugar

Try something like this:

<$let highest_counter={{{ [range[1,10]] :map[<storyTiddler>has:field<currentTiddler>then<currentTiddler>] +[maxall[]add[2]] }}}>

<$list filter="[range[1,10]] " variable="" counter="counter">
<% if [<counter>max<highest_counter>match<counter>] %> <!-- show nothing --> 
<% elseif [<storyTiddler>!has:field<counter>] %>
<$button>make counter#<<counter>> <$action-setfield $field=<<counter>> $value=""/></$button>
<% else %>
 field <<counter>>: <$select field=<<counter>> > default="1"><$list filter='[range[0,40]]'><option value=<<currentTiddler>>><$link/></option></$list></$select>
<%endif%>
</$list>
</$let>

This is a simple solution that just names the fields according to the next-in-order counter value. If you don’t want fields with names such as 1 2 3 (etc.) then you can modify as needed. (The “let” widget at the beginning just tracks where the cap is, so that you’re not showing a button to create a field more than 1 away from the max field number that already exists.)

Here’s a demo of the above in action: Quick demo — showcasing…

This is almost exactly what I am looking for the add and the range are perfect but how do i get remove them if I make too many?

I cannot stress how wonderful it is that you did this for me I very much appreciate it and the community for helping me out with this quandry.

Thank you sooo much!

Thrugar

1 Like

Check out the link again — I’ve added a button with deletefield action, and also added a bit more formatting: Quick demo — showcasing…

1 Like

Springer
You are AMAZING! thank you very much this is exactly what I was looking for! I cannot express my gratitude enough for this, thank you again!

A very grateful,
Thrugar

I tried to create a version that used an edit button, to introduce such a dropdowns wherever in the text we choose.

I failed miserably.

I don’t use edit buttons much (mostly just excise and occasionally preview) and clearly I still don’t understand them well. But in my attempts, I did come up with something that works similarly to @Springer’s version, but with enough differences to call out.

You can test by downloading the following, and uploading the resulting file onto any wiki:

Drop Down Counter.json (1.1 KB)

The big difference here is that the new id is chosen by finding all fields with the prefix nbr-, finding the maximum of their numeric suffixes, adding one, and prepending the nbr- prefix. (So, for instance, if I have nbr-7, nbr-1, and nbr-41 fields, it will create a nbr-42 one.

The relevant bit of the code is

  <$let next-nbr={{{ [<currentTiddler>fields[]removeprefix[nbr-]] [[0]] +[maxall[]add[1]] }}}>
    <$button>add #<<next-nbr>> <$action-setfield $field={{{ [<next-nbr>addprefix[nbr-]] }}} $value="0"/></$button>
  </$let>

Other than that (and my more minimal styling), this is quite similar to @Springer’s version.


Maybe I’ll try again for the edit button. Even it that’s not helpful to the OP, I’m bothered by the failure. (I just kept adding literal text, never the dynamic things that included equivalents of next-nbr.)

1 Like

Thank you for all your help but I think Springer nailed what I was looking for. Have a great weekend and again thank you for your assistance. You are appreciated!

Thrugar