Select2 (4.00) return undefined in onSelect event when using ajax

I am using Select2 (ver 4.00) and loading remote data using ajax method.

I need to get the name of the selected parameter, but in select2: select event data is not limited

my code is:

 $(".js-data-action-terms").select2({
    ajax: {
        url: ajaxurl + "?action=terms",
        dataType: 'json',
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data, page) {
            return {
                results: data.items
            };
        },
        cache: false
    },
    escapeMarkup: function (markup) {
        return markup;
    },
    minimumInputLength: 1,
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
});

$('.js-data-action-terms').on("select2:select", function(e) {
    console.log(e);
});

Results Log:

enter image description here

+4
source share
1 answer

In Select2 4.0.0, the selected object has been moved from property evt.datato evt.params.data. Now all additional data for events in Select2 is placed in evt.paramsfor consistency.

+4
source

Source: https://habr.com/ru/post/1612190/


All Articles