Ajax-Bootstrap-Select: Cannot instantiate an AjaxBootstrapSelect instance without selectpicker first being initialized!

I’m trying to use this awesome plugin in a BS modal but I got this:

Cannot instantiate an AjaxBootstrapSelect instance without selectpicker first being initialized!

a piece of code:

selectPartner = jQuery('.select-partner'); selectPartner.selectpicker({ bindEvent: 'keyup paste' }).ajaxSelectPicker({

I’ve tried wrapping ‘ajaxSelectPicker’ in a setTimout and it seems to work.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18 (7 by maintainers)

Most upvoted comments

There is option to use $(“select.your-ajax-select-class”), not only id. It works in my case.

@dmazilov 's solution works perfectly.

The reason we were seeing that error was because we call this function multiple times… First time, $(“.your-ajax-select-class”)… works just fine.

However, selectpicker puts the same class on the div it creates around the <select> for its own display

Now the next time we do $(“.your-ajax-select-class”)… It tries to add ajaxselectpicker to the div and it fails.

Hence, $(“select.your-ajax-select-class”) solves the issue

Removing livesearch will fix the error from ajaxbootstrap select, I would also try removing the bindEvent as well. AjaxBootsrapSelect takes over the input completely, passing any settings to bootstrap select may be messing with it.

How do you search for option then?

Although it is working I get again the error message on any subsequent refresh of the page after I force reload (empty cache) the page 😦

Force reload (disabling cache):

image

Normal refresh with cache use:

image

Actual code:

jQuery(document).ready(function () {
            selectPartner = jQuery('.select-partner');          
            if (selectPartner != null)
                selectPartner.selectpicker().ajaxSelectPicker({
                    ajaxOptions: {
                        url: '/api/partners',
                        dataType: 'json',
                        headers: {
                            'X-CSRF-Token': window.Laravel.csrfToken,
                            'Authorization': 'Bearer ' + Cookies.get('api_token')
                        },
                        data: function () {
                            return {
                                partner_name: jQuery('.select-partner .bs-searchbox > input').val()
                            };
                        }, beforeSend: function (xhr, opts) {
                            /*if (jQuery('.select-partner .bs-searchbox > input').val().length < 3) {
                                xhr.abort();
                            }*/
                        }
                    },
                    emptyRequest: true,
                    preprocessData: function (data) {
                        console.info('incoming_data', data);
                        if (data.partners != null)
                            return data.partners;
                        return [];
                    },
                    preserveSelected: false
                });
          [...]