Select Widget and Empty Field

  1. Goto TiddlyWiki — a non-linear personal web notebook
  2. Create a Tiddler with below text field
<$select tiddler="stemp" field="mychoice" default="One">
<$list filter="One Two Three Four Five" >
<option value=<<currentTiddler>>><$view field='title'/></option>
</$list>
</$select>
  1. Save, see the result

The select widget works as expected and shows the default value
Play with Select and then open stemp and delete the value of mychoice field, one expect to see the default value, but $select shows nothing!

How is it possible to use default value on empty field?

1 Like

Seems to work OK if you delete the field, or the entire stemp tiddler.

Given mychoice is only populated on change, you can’t rely on stemp unfortunately!

Yes, It works! but not on empty field! As you said deleting the field is one solution!

I am not so sure how you make use of the default when the field is empty, but I tend to use the default just to point to the setting, possibly a waste of time.

<$select tiddler="stemp" field="mychoice" default={{stemp!!mychoice}}>
<$list filter="One Two Three Four Five" >
<option value=<<currentTiddler>>><$view field='title'/></option>
</$list>
</$select>
  • I suppose code elsewhere may behave as if the default was selected when the field is empty.

Maybe you can wrap the whole thing in a listwidget that tests if the field is empty or doesn’t exist. And the output from this widget is the default value in the selectwidget.

What about this:

<$select tiddler="stemp" field="mychoice" default={{{[[stemp]get{!!mychoice}else[One]]}}}>
<$list filter="One Two Three Four Five" >
<option value=<<currentTiddler>>><$view field='title'/></option>
</$list>
</$select>

Thanks Fred,
It did not work!

I think $list checks for the field, so I cannot find a way to use the conditional $list here!

Look at the screenshot, the $select displays an empty drop down and it is confusing!

I found an ugly solution, delete field when it is empty! because the empty field is created by some actions

$select will show an empty choice if the contents of the existing field does not match one of the options. You could write “Six” into mychoice and would get the same result. The default attribute only guards against missing fields / tiddlers. As it says so in the docs, it’s probably by design.

You could make the empty field one of the options and have the $select show arbitrary text for it:

<$select tiddler="stemp" field="mychoice " default="One">
<option disabled value="">The field is empty.</option>
<$list filter="One Two Three Four Five" >
<option value=<<currentTiddler>>><$view field="title" /></option>
</$list>
</$select>

I can’t tell if this would be of any use in your scenario. Whereever you actually use the value of mychoice, you’d have to decide what to do with an empty field, too. The selection “The field is empty.” is purely a visual hint for the user, the actual value remains an empty string (that you could check for with is[blank]).

Have a nice day
Yaisog

PS: There is an error in the docs. In example “Simple List with Placeholder Value” it says “Note that the targeted field must be empty, or not exist, for the placeholder to show in the widget”, which, as one can easily test oneself, is not the case.

2 Likes

I am trying to use the default to start with and it is not working when the field exists but is empty anyway. I suspect there is a bug.

<$let values="One Two Three Four Five" default-value="One">
<$select field="test-field" default={{{ [all[current]!has[test-field]then<default-value>] }}}>
<$list filter='[subfilter<values>]'>
<option value=<<currentTiddler>>><$view field='title'/></option>
</$list>
</$select>

Use the above and select another value then delete the value in the field, the default is not displayed.

Unless you select other than One the first time the field is not updated

We now have the actions option perhaps to delete the field as a workaround, but when?

I am convinced this is a bug or outside the design specification;

When the select widget says “default Default value to be used if the tiddler, field or index specifies a missing value” it miss-interprets an empty field as not missing.

  • However unless you empty the field outside of the select widget it is not a fault.

See ## $:/core/modules/widgets/select.js

  • It may be because deleting the field content is insufficient for the select widget to detect a change.
  • You could place a “clear” button next to it to delete rather than blank the field

This may be answered by core developers. They may differentiate between a blank filed and a missing field.
What we asked here is to interpret a blank field as missing.

This works! Thank you @Yaisog

I think the visual hint is enough!

@jeremyruston
See the above example: The code generates a field with a trailing space: mychoice . (Note to the blank at the end of name! This is highly error prone!
I cannot remember, but I think you discussed this and you recommended to trim the field names before set!

Sorry @Mohammad could you clarify which example you mean?

Sure, please see below

The $select creates a field with trailing space i.e. field="mychoice ", most of the time this is by mistake, author added a leading or trailing spaces.

I swear I only copied it from the OP! :wink:

But yeah, that is a kind of bug that is very difficult to find.

The problem here is the attribute field="my choice " has a space at the end of the value.

This is tricky to fix.

In general, I think that the user interface should try to help people avoid shooting themselves in the foot, and so it makes sense to for example trim field names and values.

Limiting the underlying primitives so that they cannot for example create a field that ends in whitespace sounds just as reasonable, but can actually lead to subtle problems. For example, it would mean that it would be possible to encounter the situation where we assign a value to a field but that the field doesn’t end up with the desired value, and so subsequent comparisons with the field contents will not match.

1 Like

Hi Jeremy!
Thank you for Clarification!