Checkbox
Extends: LuCI.ui.AbstractElement
Source: ui.js:633
The Checkbox class implements a simple checkbox input field.
UI widget instances are usually not supposed to be created by view code directly, instead they're implicitly created by LuCI.form when instantiating CBI forms.
This class is automatically instantiated as part of LuCI.ui. To use it in views, use 'require ui' and refer to ui.Checkbox. To import it in external JavaScript, use L.require("ui").then(...) and access the Checkbox property of the class instance value.
Constructor
new Checkbox(
value?: string,
options?: LuCI.ui.Checkbox.InitOptions,
): CheckboxInstantiate a checkbox widget.
Parameters
value(string, optional, default:null) — The initial input value.options(LuCI.ui.Checkbox.InitOptions, optional) — Object describing the widget specific options to initialize the input.
Instance Methods
isChecked(): boolean
Test whether the checkbox is currently checked.
Returns
boolean— Returnstruewhen the checkbox is currently checked, otherwisefalse.
getValue(): string | Array.<string> | null
Inherited from LuCI.ui.Checkbox#getValue
Overrides: LuCI.ui.AbstractElement#getValue
Read the current value of the input widget.
Returns
string| Array.<string> | null— The current value of the input element. For simple inputs like text fields or selects, the return value type will be a - possibly empty - string. Complex widgets such asDynamicListinstances may result in an array of strings ornullfor unset values.
setValue(value: string | Array.<string> | null)
Inherited from LuCI.ui.Checkbox#setValue
Overrides: LuCI.ui.AbstractElement#setValue
Set the current value of the input widget.
Parameters
value(string | Array.<string> | null) — The value to set the input element to. For simple inputs like text fields or selects, the value should be a - possibly empty - string. Complex widgets such asDynamicListinstances may accept string array ornullvalues.
setPlaceholder(value: string | Array.<string> | null)
Inherited from LuCI.ui.AbstractElement#setPlaceholder
Set the current placeholder value of the input widget.
Parameters
value(string | Array.<string> | null) — The placeholder to set for the input element. Only applicable to text inputs, not to radio buttons, selects or similar.
isChanged(): boolean
Inherited from LuCI.ui.AbstractElement#isChanged
Check whether the input value was altered by the user.
Returns
boolean— Returnstrueif the input value has been altered by the user orfalseif it is unchanged. Note that if the user modifies the initial value and changes it back to the original state, it is still reported as changed.
isValid(): boolean
Inherited from LuCI.ui.AbstractElement#isValid
Check whether the current input value is valid.
Returns
boolean— Returnstrueif the current input value is valid orfalseif it does not meet the validation constraints.
setInvalid(message: string)
Inherited from LuCI.ui.AbstractElement#setInvalid
Mark the widget as invalid with the given message.
Used for constraints that are only checked when the form is saved, e.g. an empty required value. The marker is removed as soon as the user edits the widget.
Parameters
message(string) — The message shown as error tooltip.
getValidationError(): string
Inherited from LuCI.ui.AbstractElement#getValidationError
Returns the current validation error
Returns
string— The validation error at this time
triggerValidation(): boolean
Inherited from LuCI.ui.AbstractElement#triggerValidation
Force validation of the current input value.
Usually input validation is automatically triggered by various DOM events bound to the input widget. In some cases it is required though to manually trigger validation runs, e.g. when programmatically altering values.
Returns
boolean
registerEvents(
targetNode: Node,
synevent: string,
events: Array.<string>,
)
Inherited from LuCI.ui.AbstractElement#registerEvents
Dispatch a custom (synthetic) event in response to received events.
Sets up event handlers on the given target DOM node for the given event names that dispatch a custom event of the given type to the widget root DOM node.
The primary purpose of this function is to set up a series of custom uniform standard events such as widget-update, validation-success, validation-failure etc. which are triggered by various different widget specific native DOM events.
Parameters
targetNode(Node) — Specifies the DOM node on which the native event listeners should be registered.synevent(string) — The name of the custom event to dispatch to the widget root DOM node.events(Array.<string>) — The native DOM events for which event handlers should be registered.
setUpdateEvents(targetNode: Node, ...events: string)
Inherited from LuCI.ui.AbstractElement#setUpdateEvents
Set up listeners for native DOM events that may update the widget value.
Sets up event handlers on the given target DOM node for the given event names which may cause the input value to update, such as keyup or onclick events. In contrast to change events, such update events will trigger input value validation.
Parameters
targetNode(Node) — Specifies the DOM node on which the event listeners should be registered.events(string) — The DOM events for which event handlers should be registered.
setChangeEvents(targetNode: Node, ...events: string)
Inherited from LuCI.ui.AbstractElement#setChangeEvents
Set up listeners for native DOM events that may change the widget value.
Sets up event handlers on the given target DOM node for the given event names which may cause the input value to change completely, such as change events in a select menu. In contrast to update events, such change events will not trigger input value validation but they may cause field dependencies to get re-evaluated and will mark the input widget as dirty.
Parameters
targetNode(Node) — Specifies the DOM node on which the event listeners should be registered.events(string) — The DOM events for which event handlers should be registered.
render(): Node
Inherited from LuCI.ui.Checkbox#render
Overrides: LuCI.ui.AbstractElement#render
Render the widget, set up event listeners and return resulting markup.
Returns
Node— Returns a DOM Node or DocumentFragment containing the rendered widget markup.
Other
InitOptions: LuCI.ui.AbstractElement.InitOptions
In addition to the AbstractElement.InitOptions the following properties are recognized:
Parameters
value(string)options(object)
Properties
value_enabled(string, optional, default:1) — Specifies the value corresponding to a checked checkbox.value_disabled(string, optional, default:0) — Specifies the value corresponding to an unchecked checkbox.hiddenname(string, optional) — Specifies the HTMLnameattribute of the hidden input backing the checkbox. This is a legacy property existing for compatibility reasons, it is required for HTML based form submissions.