caption: select
created: 20131024141900000
modified: 20141203155137508
tags: Widgets
title: SelectWidget
type: text/vnd.tiddlywiki
! Introduction
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 `` and ` ` elements.
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.
In multiple selection mode, the list of selected values is bound to the specified tiddler field or index. Browsers generally use the ctrl or cmd keys for multiple selection.
For example, this select widget displays a list of the tags in this wiki:
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler=<> default='HelloThere'>
<$list filter='[all[shadows+tiddlers]tags[]sort[title]]'>
>><$view field='title'/>
$list>
$select>"/>
The <$link to=<>>state tiddler$link> currently contains:
<$edit-text tiddler=<> 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.
! Content and Attributes
The content of the `<$select>` widget should be one or more HTML `` or `` elements that provide the available values.
|!Attribute |!Description |
|tiddler |The title of the tiddler containing the value to be displayed/modified by the select widget (defaults to the current tiddler) |
|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 |
|multiple |If present, switches to multiple selection mode |
|size |The number of rows to display in multiple selection mode |
! 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'>
A Tale of Two Cities
A New Kind of Science
The Dice Man
$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'>
A Tale of Two Cities
A New Kind of Science
The Dice Man
$select>"/>
!! Option Groups
Entries in the list can be grouped together with the `` element
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/SiteTitle'>
A Tale of Two Cities
The Dice Man
A New Kind of Science
The Recursive Universe
$select>"/>
!! Generated Lists
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.
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/generated-list-demo-state'>
<$list filter='[tag[TableOfContents]]'>
<$view field='title'/>
$list>
$select>
<$tiddler tiddler={{$:/generated-list-demo-state}}>
<$transclude mode='block'/>
$tiddler>"/>
!! Nested Lists
This example uses a nested pair of list widgets. The outer one generates the `` elements, and the inner one generates `` 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]]'>
<$list filter='[all[shadows+tiddlers]prefix[$:/language/Docs/Types/]group{!!group}] +[sort[description]]'>
<$view field='description'><$view field='title'/>$view> (<$view field='name'/>)
$list>
$list>
$select>"/>
!! Multiple Selections
This example uses the `multiple` keyword to specify that we should be able to select multiple items.
<$macrocall $name="wikitext-example-without-html" src="<$select tiddler='$:/generated-list-demo-state' field='testing' multiple size='8'>
<$list filter='[tag[TableOfContents]]'>
<$view field='title'/>
$list>
$select>
<$list filter='[list[$:/generated-list-demo-state!!testing]]'>
<$view field='title' />
$list>
"/>