1
0
mirror of synced 2024-11-30 08:36:03 +03:00
select2/pages/12.programmatic-control/04.events/docs.md
2017-09-11 14:34:54 -04:00

4.7 KiB

title metadata taxonomy process never_cache_twig
Events
description
Listening to Select2's built-in events, and manually triggering events on the Select2 component.
category
docs
twig
true
true

Select2 will trigger a few different events when different actions are taken using the component, allowing you to add custom hooks and perform actions.

Event Description
change Triggered whenever an option is selected or removed.
select2:closing Triggered before the dropdown is closed. This event can be prevented.
select2:close Triggered whenever the dropdown is closed. select2:closing is fired before this and can be prevented.
select2:opening Triggered before the dropdown is opened. This event can be prevented.
select2:open Triggered whenever the dropdown is opened. select2:opening is fired before this and can be prevented.
select2:selecting Triggered before a result is selected. This event can be prevented.
select2:select Triggered whenever a result is selected. select2:selecting is fired before this and can be prevented.
select2:unselecting Triggered before a selection is removed. This event can be prevented.
select2:unselect Triggered whenever a selection is removed. select2:unselecting is fired before this and can be prevented.

Listening for events

All public events are relayed using the jQuery event system, and they are triggered on the <select> element that Select2 is attached to. You can attach to them using the .on method provided by jQuery:

$('#mySelect2').on('select2:select', function (e) {
  // Do something
});

Event data

When select2:select is triggered, data from the selection can ba accessed via the params.data property:

$('#mySelect2').on('select2:select', function (e) {
    var data = e.params.data;
    console.log(data);
});

e.params.data will return an object containing the selection properties. Any additional data for the selection that was provided in the data source will be included in this object as well. For example:

{
  "id": 1,
  "text": "Tyto alba",
  "genus": "Tyto",
  "species": "alba"
}

Triggering events

You can manually trigger events on a Select2 control using the jQuery trigger method. However, if you want to pass some data to any handlers for the event, you need to construct a new jQuery Event object and trigger on that:

var data = {
  "id": 1,
  "text": "Tyto alba",
  "genus": "Tyto",
  "species": "alba"
};

$('#mySelect2').trigger({
    type: 'select2:select',
    params: {
        data: data
    }
});

What events can be prevented? How can I prevent a selection from being made?

Examples

    
    
    

    Internal Select2 events

    Select2 has an internal event system that works independently of the DOM event system, allowing adapters to communicate with each other. This internal event system is only accessible from plugins and adapters that are connected to Select2 - not through the jQuery event system.

    You can find more information on the public events triggered by individual adapters in the advanced chapter.