This page is part of a static HTML representation of the TiddlyWiki at http://tiddlywiki.com/

CheckboxWidget

21st December 2016 at 8:28pm
Widgets

Introduction

The checkbox widget displays an HTML <input type="checkbox"> element that is dynamically bound to either:

  • the presence or absence of a specified tag on a specified tiddler
  • the value of a specified field of a specified tiddler

Content and Attributes

The content of the <$checkbox> widget is displayed within an HTML <label> element immediately after the checkbox itself. This means that clicking on the content will toggle the checkbox.

AttributeDescription
tiddlerTitle of the tiddler to manipulate (defaults to the current tiddler)
tagThe name of the tag to which the checkbox is bound
invertTagWhen set to yes, flips the tag binding logic so that the absence of the tag causes the checkbox to be checked
fieldThe name of the field to which the checkbox is bound
index New in 5.1.14 The index of the tiddler, a DataTiddler, to which the checkbox is bound
be sure to set the tiddler correctly
checkedThe value of the field corresponding to the checkbox being checked
uncheckedThe value of the field corresponding to the checkbox being unchecked
defaultThe default value to use if the field is not defined
classThe class that will be assigned to the label element
actions New in 5.1.14 A string containing ActionWidgets to be triggered when the checkbox is clicked

Tag Mode

Using the checkbox widget in tag mode requires the tag attribute to specify the name of the tag. The tiddler attribute specifies the tiddler to target, defaulting to the current tiddler if not present.

This example creates a checkbox that flips the done tag on the current tiddler:

<$checkbox tag="done"> Is it done?</$checkbox>

That renders as:

Field Mode

Using the checkbox widget in field mode requires the field attribute to specify the name of the field. The checked and unchecked attributes specify the values to be assigned to the field to correspond to its checked and unchecked states respectively. The default attribute is used as a fallback value if the field is not defined.

This example creates a checkbox that is checked if the field status is equal to open and unchecked if the field is equal to closed. If the field is undefined then it defaults to closed, meaning that the checkbox will be unchecked if the status field is missing.

<$checkbox field="status" checked="open" unchecked="closed" default="closed"> Is it open?</$checkbox><br>''status:'' {{!!status}}

That renders as:


status:

Index Mode

To use the checkbox widget in index mode set the index attribute to the index of a DataTiddler. The checked and unchecked attributes specify the values to be assigned to the index and correspond to its checked and unchecked states respectively. The default attribute is used as a fallback value if the index is undefined.

The example below creates a checkbox that is checked if the index by the name of this tiddler in the tiddler ExampleData is equal to selected and unchecked if the index is an empty string. If the index is undefined then it defaults to an empty string, meaning the checkbox will be unchecked if the index is missing.

<$checkbox tiddler="ExampleData" index=<<currentTiddler>> checked="selected" unchecked="" default=""> Selected?</$checkbox>

That renders as:

actions Attribute

This example of using the actions attribute shows both the Action Set Field Widget and Action Send Message Widget to demonstrate two actions.

The Set Widget uses a filter value to set the value of variable tag. The Action Send Message Widget joins all the tags into one large tag. The Action Set Field Widget appends the tags as individual tags. In this example, the Field Mangler Widget is required for the Action Send Message Widget but not for Action Set Field Widget. Be aware that the action occurs whether you check or uncheck.

It is often necessary to use triple quotes with the actions attribute. Alternatively, the attribute can be assigned directly from a variable with actions=<<my-actions>>.

<$fieldmangler>
<$set filter="[[Features]] [[Encryption]] +[tags[]]" name="tag">
<$checkbox actions="""<$action-setfield  $field="tags" $value=<<tag>> /><$action-sendmessage $message="tm-add-tag" $param=<<tag>> />""" field="checked" checked="YES" unchecked="NO" >
    Add tags from tiddlers [[Features]] and [[Encryption]]
</$checkbox>
</$set>
</$fieldmangler>

That renders as: