Methods

Select2 for WireBootstrap extends the wire.ui.component class. As such, it inherits all its base methods.

The following methods are added to Select2 for WireBootstrap.

Name

Description

clear

Clears the current selection(s) and restores the list to its default state

dirty(val)

Returns whether there has been a change to the default selection. Optionally sets the dirty value to val if supplied.

getSelectedItem

For single-select, returns the selected item from the config's data map

getSelectedItems

For multi-select, returns all the selected items from the data map

getSelectedRow

For single-select, returns the row from the config data parameter in which the current selection resides.

val(val)

Returns the currently selected value set in the config's datamap.id.This will be a single string or an array depending on whether select2.multiple is set.

Optionally sets the currently selected value to val if supplied.

validate

Validates the currently selected value using the rules set up in the config

The example below uses getSelectedItem to get the currently selected data map value from a list of users in the drop down per the datamap set up up in the configuration assuming jembiid was selected.

const users = [
    { user: "bsimmons", name: "Ben Simmons", isGood: false },
    { user: "jembiid", name: "Joel Embiid" , isGood: true }

const select2 = new wire.select2().render("#select", {
    data: users,
    datamap: { id: "user", text: "name" }
});

...

const user = select2.getSelectedItem();

// user = { id: "jembiid", text: "Joel Embiid" }

The example below gets the data row for the currently selected value in the same drop down using getSelectedRow.

...

const user = select2.getSelectedRow();

// user = { user: "jembiid", name: "Joel Embiid" , isGood: true }

Last updated