Dual Listbox

lightning:dualListbox

A widget that provides an input listbox, accompanied with a listbox of selectable options. Order of selected options is saved. This component requires API version 41.0 and later.

For Aura components only. For LWC development, use lightning-dual-listbox.

For Use In

Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline

A lightning:dualListbox component represents two side-by-side listboxes. Select one or more options in the list on the left. Move selected options to the list on the right. The order of the selected options is maintained and you can reorder options.

This component implements styling from Dueling Picklist in the Lightning Design System.

Here’s an example that creates a simple dual listbox with 8 options. Options 7, 2 and 3 are selected under the “Second Category” listbox. Options 2 and 7 are required options.

1<aura:component>
2
3  <aura:attribute name="listOptions" type="List" default="[]" />
4
5  <aura:attribute name="defaultOptions" type="List" default="[]" />
6
7  <aura:attribute name="requiredOptions" type="List" default="[]" />
8
9  <aura:handler name="init" value="{! this }" action="{! c.initialize }" />
10
11  <lightning:dualListbox
12    aura:id="selectOptions"
13    name="Select Options"
14    label="Select Options"
15    sourceLabel="Available Options"
16    selectedLabel="Selected Options"
17    options="{! v.listOptions }"
18    value="{! v.defaultOptions }"
19    requiredOptions="{! v.requiredOptions }"
20    onchange="{! c.handleChange }"
21  />
22
23</aura:component>

Here’s the client-side controller that loads the options and uses the onchange handler to handle value changes.

1/** Client-Side Controller **/
2({
3  initialize: function (component, event, helper) {
4    var options = [
5      { value: "1", label: "Option 1" },
6      { value: "2", label: "Option 2" },
7      { value: "3", label: "Option 3" },
8      { value: "4", label: "Option 4" },
9      { value: "5", label: "Option 5" },
10      { value: "6", label: "Option 6" },
11      { value: "7", label: "Option 7" },
12      { value: "8", label: "Option 8" },
13    ];
14    var values = ["7", "2", "3"];
15    var required = ["2", "7"];
16    component.set("v.listOptions", options);
17    component.set("v.defaultOptions", values);
18    component.set("v.requiredOptions", required);
19  },
20  handleChange: function (cmp, event) {
21    // Get the list of the "value" attribute on all the selected options
22    var selectedOptionsList = event.getParam("value");
23    alert("Options selected: '" + selectedOptionsList + "'");
24  },
25});

To specify the number of options users can select, use the min and max attributes. For example, if you set min to 3 and max to 8, users must select at least 3 options and at most 8 options.

Input Validation 

Client-side input validation is available for this component. Note that a disabled dual listbox is always valid.

To check the validity states of the input, use the validity attribute to return an object with read-only boolean attributes.

  • rangeOverflow
  • rangeUnderflow
  • valueMissing
  • valid

For more information, see lightning:input.

Error Messages 

When an input validation fails, the following messages are displayed by default.

  • rangeOverflow: Select at most [max] options
  • rangeUnderflow: At least [min] options must be selected
  • valueMissing: An option must be selected

[max] and [min] refer to the numerical values for the max and min attributes you provide.

You can override the default messages by providing your own values for these attributes: messageWhenRangeOverflow, messageWhenRangeUnderflow, and messageWhenValueMissing

Custom Validity Error Messages 

To programmatically display error messages on invalid fields, use the reportValidity() method. For custom validity error messages, display the message using setCustomValidity() and reportValidity().

Usage Considerations 

To retrieve the selected values, use the onchange handler as shown in the previous example.

The onchange handler is triggered when you click the left and right buttons to move options from one list to another or when you change the order of options in the selected options list.

For optimal performance, limit each list to 50 options or fewer. Moving more than 50 options at once can be resource-intensive and may result in noticeable delays for users.

Accessibility 

Use these keyboard shortcuts to work with dual listboxes.

  • Click - Select a single option.
  • Cmd+Click - Select multiple options or deselect selected options.
  • Shift+Click - Select all options between the current and last clicked option.

To work with options, move focus to an option and use these shortcuts.

  • Up Arrow - Move selection to previous option.
  • Down Arrow - Move selection to next option.
  • Ctrl+Up Arrow (Windows) or Cmd+Up Arrow (Mac) - Move focus to previous option.
  • Ctrl+Down Arrow (Windows) or Cmd+Down Arrow (Mac) - Move focus to next option.
  • Ctrl+Space - Toggle selection of focused option.
  • Ctrl+Right Arrow (Windows) or Cmd+Right Arrow (Mac) - Move selected options to right listbox.
  • Ctrl+Left Arrow (Windows) or Cmd+Left Arrow (Mac) - Move selected options to left listbox.
  • Tab - Move focus to the buttons or between boxes.

When focus is on an option, type a character to move focus to the next option with a name that starts with the typed character. The first typed character is matched with the first character of the label in the order the options occur. To select the focused option, press Ctrl+Space. This type-ahead feature isn’t currently supported for multibyte characters such as Chinese and Japanese.

For example, if you have multiple options that starts with the letter “E” and focus is on an option that starts with “A”, type “E” to move focus to the first option that starts with “E”. Typing “E” again moves focus to the next option that starts with “E”.

The component presents two buttons to move an option to the selected list and back to the source list.

  • The first button’s assistive text is “Move selection to Selected”, where Selected is the label of the second list.
  • The second button’s assistive text is “Move selection to Source”, where Source is the label of the first list.

When focus is on a button:

  • Move selection to Selected - Select an option from the first (source) list, then press Enter on the button to move the option to the selected list.
  • Move selection to Source - Select an option from the second (selected) list, then press Enter on the button to move the option back to the source list.

If no items are selected or available, the move buttons and reorder buttons are disabled.

To accommodate high magnification when zoom is greater than 200%, the list boxes stack vertically, separated by up and down arrow icons instead of right and left arrow buttons.

Attributes 

NameDescriptionTypeDefaultRequired
accesskeySpecifies a shortcut key to activate or focus an element.String
addButtonLabelLabel for add button.StringMove selection to {selectedLabel}
bodyThe body of the component. In markup, this is everything in the body of the tag.Aura.Component[]
disabledSpecifies that an input element should be disabled. This value defaults to false.Booleanfalse
disableReorderingSet to true to hide the Up and Down buttons used for reordering the Selected list items.Boolean
downButtonLabelLabel for down button.StringMove selection down
fieldLevelHelpHelp text detailing the purpose and function of the dual listbox.String
labelLabel for the dual listbox.String
maxMaximum number of options required in the selected options listbox.Integer
messageWhenRangeOverflowError message to be displayed when a range overflow is detected.Boolean
messageWhenRangeUnderflowError message to be displayed when a range underflow is detected.Boolean
messageWhenValueMissingError message to be displayed when the value is missing and input is required.Boolean
minMinimum number of options required in the selected options listbox.Integer
nameSpecifies the name of an input element.String
onblurThe action triggered when the element releases focus.Aura.Action
onchangeThe action triggered when a value attribute changes.Aura.Action
onfocusThe action triggered when the element receives focus.Aura.Action
optionsA list of options that are available for selection. Each option has the following attributes: label and value.Object[]
readonlySpecifies that an input field is read-only. This value defaults to false.Booleanfalse
removeButtonLabelLabel for remove button.StringMove selection to {sourceLabel}
requiredSpecifies that an input field must be filled out before submitting the form. This value defaults to false.Booleanfalse
requiredOptionsA list of required options that cannot be removed from selected options listbox. This list is populated with values from options attribute.List
selectedLabelLabel for selected options listbox.String
showActivityIndicatorDisplays a spinner to indicate activity in the listbox. This value defaults to false.Boolean
sizeNumber of items that display before vertical scrollbars are displayed for the listboxes. Determines the vertical size of the dual listbox.Integer
sourceLabelLabel for source options listbox.String
tabindexSpecifies the tab order of an element when the Tab key is used for navigating. The tabindex value can be set to 0 or -1. The default is 0, which means that the component is focusable and participates in sequential keyboard navigation. -1 means that the component is focusable but does not participate in keyboard navigation.Integer
upButtonLabelLabel for up button.StringMove selection up
validityRepresents the validity states that an element can be in, with respect to constraint validation.Object
valueSpecifies the value of an input element.Object
valuesA list of default options that are included in the selected options listbox. This list is populated with values from the options attribute.List
variantThe variant changes the appearance of an input field. Accepted variants include standard, label-inline, label-hidden, and label-stacked. This value defaults to standard, which displays the label above the field. Use label-hidden to hide the label but make it available to assistive technology. Use label-inline to horizontally align the label and input field. Use label-stacked to place the label above the input field.Stringstandard

Methods 

NameDescriptionArgument NameArgument TypeArgument Description
checkValidityReturns the valid property value (Boolean) on the ValidityState object to indicate whether the dual listbox has any validity errors.
focusSets focus on the element.
reportValidityDisplay error messages if the dual listbox is invalid.
setCustomValiditySets a custom error message to be displayed when the dual listbox value is submitted.messageStringThe string that describes the error. If message is an empty string, the error message is reset.
showHelpMessageIfInvalidShows the help message if the form control is in an invalid state.