Something I cant figure out - why is the named tiddler field not being updated

I am trying to do something very simple, implement a look up table so that when the user enters the code for a state, eg. SA, the look up table finds the correct label, South Australia.

I have created a tiddler for each record in the lookup table, with two fields, the code and the name.

The problem is that in my code, although the state code is correctly updated through the code, the name of the state is not.

State Code: <$select tiddler='$:/TLS/selectedstate' field='state-code'>
{{$:/TLS/state_for_select}}
</$select>

-----

<$button>

<$set name="selectedstatecode" value={{$:/TLS/selectedstate!!state-code}} >

     <$list filter="[all[tiddlers]field:statecode<selectedstatecode>]">

         <$set name="selectedstatename" value={{!!statename}} >

               <$action-setfield $tiddler="$:/TLS/selectedstate" $field="state-name" $value={{!!statename}} />
        
    </$list> 

<$set name="stateTag" value={{$:/TLS/selectedstate!!state-name}}>
<$set name="locationTag" value={{$:/TLS/location}}>
<$set name="typeTag" value={{$:/TLS/type}} >
<$set name="cityTag" value={{$:/TLS/city}} >


     <$action-createtiddler 
          $basetitle={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{$:/TLS/selectedstate!!state-name}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}}
          tags={{{ [[All Items]] [[Needs History]] [<cityTag>] [<stateTag>] [<locationTag>] [<typeTag>] +[format:titlelist[]join[ ]] }}}

So if I select the state code SA, as you can see from the image, the selectedstate!!state-name field is not correctly updated.

After the first $set, the selectedstatecode variable is ‘SA’

After the $list, currentTiddler points to the $:/TLS/State/SouthAustralia tiddler.

After the second <$set statement, selectedstatename variable is set to South Australia

After the <$action-setfield statement, the field state-name of the tiddler $:/TLS/selectedstate is not set to South Australia

I can not explain this but I am obviously doing something wrong.

I have tried using the variable selectedstatename, which is correctly set to South Australia, in the action-setfield statement, but this still does not update the tiddler field.

Note the two different spellings statename and state-name, they are different fields in different tiddlers

Could someone point out my mistake please.

bobj

Variables inside buttons are only updated when the button is drawn. That’s why we suggest “best practice” like this

\procedure my-stuff()
<!-- your stuff comes here >
\end

<$button actions=<<my-stuff>>>Click me</$button>

Variables in my-stuff procedure are updated when the button is clicked. That makes a difference.

@pmario, thank you for your suggestion. However, that is not the behaviour I am seeing. When I select a new state code, the variables inside the button are dynamically being updated but the action-set file is not updating the tiddler field.

Bobj

You may be running into a problem I had.

\define tv-action-refresh-policy() always

means all tiddlers are updated, instead of only the first one, with action triggers.

It’s use is documented, and the main caveat to be aware of it can’t be defined globally as that interferes with the core.

Hi @Bob_Jansen

Here is a working code, at least in my limited test case:

State Code: <$select tiddler='$:/TLS/selectedstate' field='state-code'>
{{$:/TLS/state_for_select}}
</$select>

-----

<$button>Go!

<$set name="selectedstatecode" value={{$:/TLS/selectedstate!!state-code}} >

     <$list filter="[all[tiddlers]field:statecode<selectedstatecode>]">

         <$set name="selectedstatename" value={{!!statename}} >

               <$action-setfield $tiddler="$:/TLS/selectedstate" $field="state-name" $value={{!!statename}} />

        </$set>  <!-- This one was missing -->

    </$list>

</$set>

<$let
  stateTag={{$:/TLS/selectedstate!!state-name}}
  locationTag={{$:/TLS/location}}
  typeTag={{$:/TLS/type}}
  cityTag={{$:/TLS/city}}
>


     <$action-createtiddler 
          $basetitle={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{$:/TLS/selectedstate!!state-name}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}}
          tags={{{ [[All Items]] [[Needs History]] [<cityTag>] [<stateTag>] [<locationTag>] [<typeTag>] +[format:titlelist[]join[ ]] }}}/>
</$let>
</$button>

Notes:

  • There was a missing </$set> between the <$action-setfield> and the </$list>. This was the culprit in my testbed.
  • I replaced the nested set statements with a single let for clarity.
  • With this code the <$action-createtiddler> is working, although I couldn’t create a fully working test-bed. At least the “statecode” and “statename” part of the title are correct.
  • IMHO you should keep field names consistent along your code. Here you use state-name and statename for the same data, state-code and statecode, etc. This is error-prone, so I advise you always use the same field name for the same information.

Fred

1 Like

@tw-FRed

I have changed the nested $set statements to the $let code as you suggested. This now sort of works. The state name of the previous add is now added to the new addition, so @pmario may have provided the other half of the solution.

Will now have to change the code along the lines @pmario suggested.

Thanks both for your input.

bobj

@pmario I have followed the example you pointed to and moved the code out of the $button and into a procedure. However, it does not fix the problem, the statename added as a tag is the previous name although after pressing the button, the state name for the selected code is correctly stored in the selected-state tiddler. So as you intimated, it is a timing issue of some sort.

This now the complete code of the tiddler:

\procedure addProc()
<$let 
     selectedstatecode={{$:/TLS/selectedstate!!state-code}} 
>

     <$list filter="[all[tiddlers]field:statecode<selectedstatecode>]">

         <$let selectedstatename={{!!statename}} >

               <$action-setfield $tiddler="$:/TLS/selectedstate" $field="state-name" $value={{!!statename}} />
          </$let>
    </$list> 

<$let
  stateTag={{$:/TLS/selectedstate!!state-name}}
  locationTag={{$:/TLS/location}}
  typeTag={{$:/TLS/type}}
  cityTag={{$:/TLS/city}}
>

     <$action-createtiddler 
          $basetitle={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{$:/TLS/selectedstate!!state-name}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}}
          tags={{{ [[All Items]] [[Needs History]] [<cityTag>] [<stateTag>] [<locationTag>] [<typeTag>] +[format:titlelist[]join[ ]] }}}
          CityTown={{$:/TLS/city}}
          Location={{$:/TLS/location}}
          DateofManufacture={{$:/TLS/dateofmanufacture}}
          Manufacturer={{$:/TLS/manufacturer}}
          SerialNumber={{$:/TLS/serialnumber}}
          WeightofProjectile={{$:/TLS/weightofprojectile}}
          Range={{$:/TLS/range}}
          imageURL={{$:/TLS/imageURL}}
          Calibre={{$:/TLS/calibre}}
          Coordinates={{$:/TLS/coordinates}}
           $template="$:/TLS/CreateTemplateItems"
     />

<$action-navigate $to={{{ [{$:/TLS/incremental}addsuffix[:]addsuffix{$:/TLS/selectedstate!!state-name}addsuffix[ - ]addsuffix{$:/TLS/type}addprefix{$:/TLS/selectedstate!!state-code}] }}} />

<$action-setfield $tiddler="$:/TLS/dateofmanufacture" text=""/>
<$action-setfield $tiddler="$:/TLS/city" text=""/>
<$action-setfield $tiddler="$:/TLS/location" text=""/>
<$action-setfield $tiddler="$:/TLS/manufacturer" text=""/>
<$action-setfield $tiddler="$:/TLS/serialnumber" text=""/>
<$action-setfield $tiddler="$:/TLS/weightofprojectile" text=""/>
<$action-setfield $tiddler="$:/TLS/range" text=""/>
<$action-setfield $tiddler="$:/TLS/caption" text=""/>
<$action-setfield $tiddler="$:/TLS/imageURL" text=""/>
<$action-setfield $tiddler="$:/TLS/coordinates" text=""/>
<$action-setfield $tiddler="$:/TLS/calibre" text=""/>
<$action-deletetiddler $tiddler="$:/TLS/type" />


<$action-setfield $tiddler="$:/TLS/incremental" text={{{ [{$:/TLS/incremental}add[1]] }}}/>

</$let>
</$let>
\end

Add the information for the new item below and press the //Add this item// button

The asset number for this new object will be {{{ [{$:/TLS/incremental}] }}}

State Code: <$select tiddler='$:/TLS/selectedstate' field='state-code'>
{{$:/TLS/state_for_select}}
</$select>
OS for overseas, PRI for private

<$reveal type="match" stateTitle="$:/TLS/selectedstate" stateField="state-code" text="-">
Error - the selected state code can not be '-'
</$reveal>

<$reveal type="nomatch" stateTitle="$:/TLS/selectedstate" stateField="state-code" text="-">

City/Town: <$edit-text tiddler="$:/TLS/city" tag="input"/>

Location: <$edit-text tiddler="$:/TLS/location" tag="input"/>

Type:  <$edit-text tiddler="$:/TLS/type" tag="input" /> If unknown, enter 'type unknown'

Coordinates: <$edit-text tiddler="$:/TLS/coordinates" tag="input" overwrite="yes"/>

Date of Manufacture: <$edit-text tiddler="$:/TLS/dateofmanufacture" tag="input"/>

Manufacturer: <$edit-text tiddler="$:/TLS/manufacturer" tag="input" overwrite="yes"/> 

Serial Number: <$edit-text tiddler="$:/TLS/serialnumber" tag="input" overwrite="yes"/>  

Weight of Projectile: <$edit-text tiddler="$:/TLS/weightofprojectile" tag="input" overwrite=“yes” />

Range: <$edit-text tiddler="$:/TLS/range" tag="input" overwrite="yes"/>

Calibre: <$edit-text tiddler="$:/TLS/calibre" tag="input" overwrite="yes"/>

Image URL: <$edit-text tiddler="$:/TLS/imageURL" tag="input" overwrite="yes"/>

-----

<$button actions=<<addProc>>>
Add new Item
</$button>
</$reveal>


Can you advise please.

bobj

Looking further at the code and wondering why each tag, except the stateTag are correctly added to the new tiddler.

The only difference I can see is that the other tags, except for the All Items and Needs History, have their contents set through the edit-text widget as part of form filling. stateTag gets its value from an action-setfield widget

Maybe this is the issue?

To get around this, can someone suggest another way of doing a lookup table?

two fields, state-code, state-name eg. SA,South Australia

bobj

I’m wondering if you could step back a bit and describe what you want to do, ignoring for the moment, how your current attempt is going?

I’m going to take a stab at it, and it’s almost certainly wrong in some details. If it’s close, could you correct it? If it’s too far off, could you take swing at creating one from scratch?

Here’s my attempt:

"I would like to build an entry screen to create structured tiddlers with a number of fields. Most of my input fields are plain text, but the state field should be a dropdown, with options taken from those tiddlers that have a statecode field, using their statename property for display. A few other sample input fields are City/Town (city), Type (type), Date of Manufacture (DateofManufacture) and Image URL (imageURL). There are about a dozen such fields in total.

"The generated title would look like this:

NSW1003:New South Wales - Howitzer
`-'`--' `-------------'   `------'
 |  |          |              `------- type
 |  |          `---------------------- state name
 |  `--------------------------------- sequence number
 `------------------------------------ state code

"where the sequence number is automatically incremented for each new entry. (I think I know how to do this part.)

"These all receive the two tags All Items and Needs History, and they also get tags for the state, town/city, and type. The remaining inputs just become fields on the generated tiddler. (This is also working properly.)

"On creation of the new tiddler, the input fields should all revert to blank values.

"Here is my attempt so far:

"    (either a link to a wiki online or a JSON file that can be dragged onto another wiki.)

"Much of the above is working. But I’m having a problem with the state name. When I click the button, the state code is updated properly in the output, but the state name is not. It seems to be happening in…(further explanation)

“Do you have any suggestions for how I can fix this issue? Or is there a better way to achieve the overall goal?”

I think something like that would help us help you.

@Scott_Sauyet , you’ve got most of it. here is a link to the wiki,

The tiddler in question is $:/TLS/Actions/Add New Item

Bobj

I am closing this thread as it is getting too confusing. Will start a new one and provide what @Scott_Sauyet suggested with a cut down wiki.

bobj

I didn’t see your new thread, so I’m responding here. If it’s already there, then I can move this post.

I tried your form from scratch, and had to make several supporting changes along the way. I have no idea if this version is of interest to you, but I learned some things doing it, so it’s a win for me either way.

My copy is at http://scott.sauyet.com/Tiddlywiki/Demo/artillery.html.

I’ve rewritten the entry form from scratch, with some cosmetic changes and a number of fundamental changes to how it works. If I did it right, the functionality will stay the same. It looks like this:

The formatting is my own idea of tidiness, and is certainly not essential. Here I use a simple table for layout. There are plenty of good alternatives. The first row of the table is a “Clear” button. I prefer this to automatically clearing the fields on submission… The last row is the “Add” button. I added a stylesheet to handle some simple formatting of this table.

To handle state names, I first added statecode fields to each of the state tiddlers. Then I updated $:/TLS/state_for_select to use the derived list of states from a filter, like this:

  <option value="" disabled="disabled">-- Please Choose --</option>
<$list filter="[tag[States]] -Overseas -Private">
  <option value={{!!statecode}}>{{!!title}}</option>
</$list>
  <hr/>
  <option value="OS">Overseas</option>
  <option value="PRI">Private</option>

I did not use the list-after from your list, choosing instead to add an <hr/> separator and hardcoding the two special pseudo-states. I also added a “Please Choose” disabled option for when there is no value selected. I had never seen the technique of promoting the list of options to its own tiddler and including that in various Select widgets. I will definitely be using that in the future! Here I reused it in a field editor (which is hooked using with a field editor filter.) This allows us the same dropdown when we’re editing the tiddler’s fields as we have on our entry screen. (There is another field editor for the id field, which disables editing.)

I chose to include this id among the the fields. (I would actually suggest that you might want to rethink the titles, using a simple title, such as Item-6, with a title cascade entry to show all the fields you want in whatever structured manner you choose. I didn’t go there with this version.) This id is calculated every time by adding one to the maximum ids of all of the items.

I also moved the holders for all the temporary variables from the text of tiddlers such as $:/TLS/calibre or $:/TLS/city to fields on a single tiddler in the temporary namespace, $:/temp/TLS. I find this much cleaner. And now my Clear button can simply delete that tiddler.

The actual logic to build the new tiddler is much the same as the original, except that there are new handlers for the state, and a new location for the temporary data.

While I deleted many of the tiddlers that moved to the temporary space, there are many others tiddlers in the $:/TLS namespace that may no longer be necessary. I didn’t spend any time on them.

In the end, I think this is a cleaner way to do things. It doesn’t even try to answer your question about what went wrong in your original; I think it simply sidesteps the issue. But I hope it’s useful.

1 Like

@Scott_Sauyet , your ideas, as in the other thread, are very welcome. I will look at your example especially for the formatting of the form.

As for the data model, now I’ve come across data tiddlers, I’d probably use them to store the details of an item as an object, just as I would in ‘normal’ coding systems.

I am not going to analyse the original attempt at this form, I’ve moved on thanks to you, @CasperBooWisdom and @EricShulman .

Thanks again.

Bobj

1 Like

Thanks @Scott_Sauyet , your example is most illuminating.

bobj

1 Like