Work with ComboBox
Setting/getting value(s)
In order to select options in ComboBox, use the setValue() method. It takes one parameter:
- value - (string|number|array) the ID(s) of Combo options from data collection that should be selected in ComboBox
// select one option
combo.setValue(combo.data.getId(1));
// selects several options at once
combo.setValue([combo.data.getId(1), combo.data.getId(3)]);
Related sample: Combobox. Set value
To get the list of selected options, apply the getValue() method. It returns IDs of selected options either as string(s) (by default) or as an array of strings. In the latter case you need to pass the following parameter:
- asArray - (boolean) true, to return ids as an array of strings
const id = combo.getValue();
// -> "u1556098582074"
// returns ids of selected options as strings
const ids = combo.getValue();
// -> "u1556097609214,u1556097609228,u1556097609244"
// returns ids of selected options as an array of strings
const array_ids = combo.getValue(true);
// -> ["u1556097609214", "u1556097609228", "u1556097609244"]
Related sample: Combobox. Get value
Disabling/enabling ComboBox
To disable/enable ComboBox, make use of the disable()/enable() methods:
// disabling a combo box
combo.disable();
// enabling a combo box
combo.enable();
Related sample: Combobox. Enable, disable, is disabled
Checking if a combobox is disabled
To check if a combobox is disabled, call the isDisabled() method:
combo.isDisabled(); // -> true/false
Related sample: Combobox. Enable, disable, is disabled
Repainting ComboBox
You can repaint ComboBox on a page, e.g. after changing its configuration, using the paint()
method:
// disabling ComboBox via the configuration object
combo.config.disabled = true;
// repaints ComboBox with a new config
combo.paint();
Clearing input
It is possible to clear the ComboBox input from the selected value(s) with the help of the clear()
method:
combo.clear();
Related sample: Combobox. Clear
Setting/removing focus
When needed, you can set focus in the ComboBox input without opening the list of options. Use the focus()
method for this purpose:
combo.focus();
To remove focus from Combobox, apply the blur()
method:
combo.blur();
Related sample: Combobox. Focus
Hiding/showing the popup
You can manipulate the visibility of the Combo popup with the help of the Popup API.
To hide/show the popup, use the corresponding methods:
show() | shows a popup in a Combo. Takes two parameters:
|
hide() | hides a popup |
// showing a popup
combo.popup.show(container);
// hiding a popup
combo.popup.hide();
Related sample: Combobox. Show / hide options list
To control the process of showing/hiding the popup, apply the related events:
beforeShow | fires before a popup is shown. The handler function takes one parameter:
|
afterShow | fires after a popup is shown. The handler function takes one parameter:
|
beforeHide | fires before a popup is hidden. The handler function takes two parameters:
|
afterHide | fires after a popup is hidden. The handler function takes one parameter:
|
combo.popup.events.on("BeforeShow", function(HTMLElement){
console.log("A popup will be shown");
return true;
});
combo.popup.events.on("AfterShow", function(HTMLElement){
console.log("A popup is shown");
});
combo.popup.events.on("BeforeHide", function(fromOuterClick,e){
console.log("A popup will be hidden");
return true;
});
combo.popup.events.on("AfterHide", function(e){
console.log("A popup is hidden");
});
Related sample: Combobox. Popup Events
Using Data Collection API
You can manipulate ComboBox options with the help of the Data Collection API.
Adding options into ComboBox
It is possible to add more options into the initialized ComboBox on the fly. Use the add()
method of Data Collection. It takes two parameters:
config | (object) the configuration object of the added option |
index | (number) optional, the position to add an option at |
For instance:
combobox.data.add({value:"Russia"},1);
Related sample: Combobox. Add data and remove item
From v7.3, Combobox includes the addOption() method which you can also use for adding new options into the data collection.
Updating ComboBox options
You can change config options of the option via the update()
method of Data Collection. It takes two parameters:
id | the id of the option |
config | an object with new configuration of the option |
For example, you can change the image of an option:
combo.data.update("option_id",{
value:"Russia", src: "../common/flags/ru.png"
});
Related sample: Combobox. Update
Removing options from ComboBox
To remove an option, make use of the remove()
method of Data Collection. Pass the id of the option that should be removed to the method:
combo.data.remove("option_id");
Check the full list of Data collection API