LuCI.ui. DynamicList

The DynamicList class implements a widget which allows the user to specify an arbitrary amount of input values, either from free formed text input or from a set of predefined choices.

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.DynamicList. To import it in external JavaScript, use L.require("ui").then(...) and access the DynamicList property of the class instance value.

Constructor

new DynamicList(valueopt, choicesopt, optionsopt)

Instantiate a dynamic list widget.

Parameters:
NameTypeAttributesDefaultDescription
valuestring | Array.<string><optional>
null

The initial input value(s).

choicesObject.<string, *><optional>

Object containing the selectable choices of the widget. The object keys serve as values for the different choices while the values are used as choice labels. If omitted, no default choices are presented to the user, instead a plain text input field is rendered allowing the user to add arbitrary values to the dynamic list.

optionsLuCI.ui.DynamicList.InitOptions<optional>

Object describing the widget specific options to initialize the dynamic list.

Extends

Methods

addChoices(values, labels)

Add new suggested choices to the dynamic list.

This function adds further choices to an existing dynamic list, ignoring choice values which are already present.

Parameters:
NameTypeDescription
valuesArray.<string>

The choice values to add to the dynamic lists suggestion dropdown.

labelsObject.<string, *>

The choice label values to use when adding suggested choices. If no label is found for a particular choice value, the value itself is used as label text. Choice labels may be any valid value accepted by LuCI.dom#content.

clearChoices()

Remove all existing choices from the dynamic list.

This function removes all preexisting suggested choices from the widget.

getValidationError() → {string}

Returns the current validation error

Returns:

The validation error at this time

Type: 
string

getValue() → {string|Array.<string>|null}

Read the current value of the input widget.

Returns:

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 as DynamicList instances may result in an array of strings or null for unset values.

Type: 
string | Array.<string> | null

isChanged() → {boolean}

Check whether the input value was altered by the user.

Returns:

Returns true if the input value has been altered by the user or false if 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.

Type: 
boolean

isValid() → {boolean}

Check whether the current input value is valid.

Returns:

Returns true if the current input value is valid or false if it does not meet the validation constraints.

Type: 
boolean

registerEvents(targetNode, synevent, events)

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:
NameTypeDescription
targetNodeNode

Specifies the DOM node on which the native event listeners should be registered.

syneventstring

The name of the custom event to dispatch to the widget root DOM node.

eventsArray.<string>

The native DOM events for which event handlers should be registered.

render() → {Node}

Render the widget, set up event listeners and return resulting markup.

Returns:

Returns a DOM Node or DocumentFragment containing the rendered widget markup.

Type: 
Node

setChangeEvents(targetNode, …events)

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:
NameTypeAttributesDescription
targetNodeNode

Specifies the DOM node on which the event listeners should be registered.

eventsstring<repeatable>

The DOM events for which event handlers should be registered.

setPlaceholder(value)

Set the current placeholder value of the input widget.

Parameters:
NameTypeDescription
valuestring | Array.<string> | null

The placeholder to set for the input element. Only applicable to text inputs, not to radio buttons, selects or similar.

setUpdateEvents(targetNode, …events)

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:
NameTypeAttributesDescription
targetNodeNode

Specifies the DOM node on which the event listeners should be registered.

eventsstring<repeatable>

The DOM events for which event handlers should be registered.

setValue(value)

Set the current value of the input widget.

Parameters:
NameTypeDescription
valuestring | 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 as DynamicList instances may accept string array or null values.

triggerValidation() → {boolean}

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:
Type: 
boolean

Type Definitions

InitOptions

In case choices are passed to the dynamic list constructor, the widget supports the same properties as Dropdown.InitOptions but enforces specific values for some dropdown properties.

Properties
NameTypeAttributesDefaultDescription
multipleboolean<optional>
false

Since dynamic lists never allow selecting multiple choices when adding another list item, this property is forcibly set to false.

optionalboolean<optional>
true

Since dynamic lists use an embedded dropdown to present a list of predefined choice values, the dropdown must be made optional to allow it to remain unselected.