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