ssuhat
ssuhat

Reputation: 7656

Select2 get html data attribute

I'm using latest select2 version.

But stuck at getting <select> html attribute.

<select name="category" id="category-select"
    data-ajax--search="slug" class="select2 form-control m-b-2" multiple>
</select>

// SCRIPT 

const opts = {
    ajax: {
        url: "/menus/search",
        delay: 250,
        data: function (param) {
            return {q: param.term, page: param.page, category: $(this.element).data('search')};
        },
        processResults: ({data, total_count}, {page}) => {
            page = page || 1;

            return {
                results: data,
                pagination: {
                    more: (page * 30) < total_count
                }
            };
        },
        cache: true
    },
    scapeMarkup: markup => markup,
    templateResult: formatRepo,
    templateSelection: formatRepoSelection
};
$('.select2').select2(opts); 

I followed this solution How to get data attribute of <select> for use in select2 from inside .select2() ajax call? and here https://github.com/select2/select2/issues/3361 but still no luck.

my data-ajax--search always return undefined.

Any solution?

Upvotes: 1

Views: 955

Answers (1)

BQ Kh&#225;nh
BQ Kh&#225;nh

Reputation: 96

Try using $(this).data('ajax-Search') instead of $(this.element).data('search')

Upvotes: 1

Related Questions