2015-11-13 12:53:19 +00:00
caption: select
created: 20131024141900000
modified: 20141203155137508
tags: Widgets
title: SelectWidget
type: text/vnd.tiddlywiki
2014-05-31 17:37:43 +00:00
! Introduction
2014-11-29 18:59:31 +00:00
The select widget displays a popup menu based on a [[HTML select element|https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select]]. The popup (or dropdown) contains a list of items defined by `<option>` and `<optgroup>` elements.
2014-12-03 17:29:32 +00:00
Every time the user selects a new value in the menu, the selected value is written to the text of a specified tiddler field or index. If the tiddler value changes the menu is automatically updated to reflect the new value.
2014-05-31 17:37:43 +00:00
2016-01-05 18:48:21 +00:00
In multiple selection mode, the list of selected values is bound to the specified tiddler field or index. Browsers generally use the <kbd>ctrl</kbd> or <kbd>cmd</kbd> keys for multiple selection.
2014-05-31 17:37:43 +00:00
For example, this select widget displays a list of the tags in this wiki:
2014-09-19 14:28:08 +00:00
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler=<<qualify 'select-demo'>> default='HelloThere'>
2014-05-31 17:37:43 +00:00
<$list filter='[all[shadows+tiddlers]tags[]sort[title]]'>
<option value=<<currentTiddler>>><$view field='title'/></option>
</$list>
</$select>"/>
2014-09-19 14:28:08 +00:00
The <$link to=<<qualify "select-demo">>>state tiddler</$link> currently contains:
<$edit-text tiddler=<<qualify "select-demo">> tag="input" default=""/>
See the text change as you switch entries in the select widget. Try changing the value of the state tiddler and see the select widget change. Notice how the select widget only displays an entry if there is a precise match with the tiddler text.
2014-05-31 17:37:43 +00:00
! Content and Attributes
The content of the `<$select>` widget should be one or more HTML `<option>` or `<optiongroup>` elements that provide the available values.
|!Attribute |!Description |
2014-09-22 09:42:41 +00:00
|tiddler |The title of the tiddler containing the value to be displayed/modified by the select widget (defaults to the current tiddler) |
2014-05-31 17:37:43 +00:00
|field |The field name for the value in the current tiddler (defaults to "text") |
|index |The index of a property in a [[DataTiddler|DataTiddlers]] (takes precedence over the field attribute) |
|class |CSS classes to be assigned to the HTML select element |
|default |Default value to be used if the tiddler, field or index specifies a missing value |
2016-01-05 18:48:21 +00:00
|multiple |If present, switches to multiple selection mode |
|size |The number of rows to display in multiple selection mode |
2014-05-31 17:37:43 +00:00
! Examples
!! Simple Lists
This example sets the title of the current wiki [[$:/SiteTitle]] to one of a list of book titles:
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/SiteTitle'>
<option>A Tale of Two Cities</option>
<option>A New Kind of Science</option>
<option>The Dice Man</option>
</$select>"/>
!! Value lists
In this example the `value` attribute has been used to specify the text that should be used as the value of the entry instead of the display text.
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/SiteTitle'>
<option value='cities'>A Tale of Two Cities</option>
<option value='science'>A New Kind of Science</option>
<option value='dice'>The Dice Man</option>
</$select>"/>
!! Option Groups
Entries in the list can be grouped together with the `<optgroup>` element
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/SiteTitle'>
<optgroup label='Fiction'>
<option value='cities'>A Tale of Two Cities</option>
<option value='dice'>The Dice Man</option>
</optgroup>
<optgroup label='Non-fiction'>
<option value='science'>A New Kind of Science</option>
<option value='recursive'>The Recursive Universe</option>
</optgroup>
</$select>"/>
!! Generated Lists
2014-10-28 16:13:00 +00:00
The ListWidget can be used to generate the options for a select widget. For example, here we combine a select widget listing all the tiddlers tagged ''TableOfContents'' with a transclusion to display the text of the selected one.
2014-05-31 17:37:43 +00:00
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/generated-list-demo-state'>
2014-10-28 16:13:00 +00:00
<$list filter='[tag[TableOfContents]]'>
2014-05-31 17:37:43 +00:00
<option><$view field='title'/></option>
</$list>
</$select>
<$tiddler tiddler={{$:/generated-list-demo-state}}>
<$transclude mode='block'/>
</$tiddler>"/>
2014-06-06 08:33:24 +00:00
!! Nested Lists
This example uses a nested pair of list widgets. The outer one generates the `<optgroup>` elements, and the inner one generates `<option>` elements:
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/generated-list-demo-nestedstate' field='type' default='text/vnd.tiddlywiki'>
<$list filter='[all[shadows+tiddlers]prefix[$:/language/Docs/Types/]each[group]sort[group]]'>
<optgroup label={{!!group}}>
<$list filter='[all[shadows+tiddlers]prefix[$:/language/Docs/Types/]group{!!group}] +[sort[description]]'>
<option value={{!!name}}><$view field='description'><$view field='title'/></$view> (<$view field='name'/>)</option>
</$list>
</optgroup>
</$list>
</$select>"/>
2015-11-13 12:50:34 +00:00
!! Multiple Selections
This example uses the `multiple` keyword to specify that we should be able to select multiple items.
2016-01-05 18:48:21 +00:00
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/generated-list-demo-state' field='testing' multiple size='8'>
2015-11-13 12:50:34 +00:00
<$list filter='[tag[TableOfContents]]'>
<option><$view field='title'/></option>
</$list>
</$select><br />
<$list filter='[list[$:/generated-list-demo-state!!testing]]'>
<$view field='title' /><br />
</$list>
"/>