Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting to destroy selectpicker and reinitialize ajaxselect creates standard select #196

Open
coreynbryant opened this issue Aug 13, 2020 · 2 comments

Comments

@coreynbryant
Copy link

I was hoping someone could point me in the right direction here. I am using two different selectpickers. The first select changes values in my ajaxselect selctpicker. It is working fine but I noticed that because I am using the cache from ajax select it is possible to get incorrect data to the user.

I am attempting to destroy the selectpicker and then reinit it when the first select is changes.bs.select but my ajax select never comes back and it just stays as a standard bootstrap select.

    var $selectOne = $('#selectOne');
    var $selectTwo = $('#selectTwo');
    var options = {
        ajax: {
            url: 'ajax',
            type: "GET",
            dataType: "json",
            contentType: 'json',
            data: function () {
                var params = {
                    term: '{{{q}}}'                       
                };

                params.changedValue = parseInt($('#selectOne').val()) > 0 ? parseInt($('#selectOne').val()) : null;

                return params;
            }
        },
        locale: {
            searchPlaceholder: 'Search for stuff...',
            statusInitialized: 'Start typing to begin search'
        },
        clearOnEmpty: false,
        log: 4,
        minLength: 2,
        requestDelay: 500,
        preserveSelected: false
    };

    $selectOne.on('changed.bs.select', function () {
        $selectTwo.selectpicker('destroy');
        $selectTwo.selectpicker({ liveSearch: true }).ajaxSelectPicker(options);
    });
@ourcoteam
Copy link

Same here

@truckingsim
Copy link
Owner

@coreynbryant yeah we don't really support that out of the box. The thing Ajax-Bootstrap-Select is looking for on initialize is the jquery data property AjaxBootstrapSelect. If you do:

$selectOne.on('changed.bs.select', function () {
        $selectTwo.selectpicker('destroy');
        $selectTow.removeData('AjaxBootstrapSelect');
        $selectTwo.selectpicker({ liveSearch: true }).ajaxSelectPicker(options);
    });

instead it should let Ajax-Bootstrap-Select go through its normal init. I'm not sure to be honest if this will have other side effects but it should force the re-init.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants