0

I have loop on my select dropdown by default it shows the data properly.

<select name="" id="input" class="form-control selectteam">
    <option value="" disabled="" selected="">Filter by team</option>
    <option v-for="(team, index) in  nblTeams" :value="team.clubName">{{team.clubName}}</option>
</select>

but when I add $('.selectteam').selectpicker(); in the mounted method as per code below

mounted: function() {
        this.getCurrentSeasonTeams().then( (response) => {
            if( response.status == 200 && typeof(response.data) == 'object' ) {
                this.nblTeams = response.data;
            }
        });

        $('.selectteam').selectpicker();
    }

it doesnt show the teams list already. Btw I'm using bootstrap-select - Silvio Moreto

2
  • How and where are you running the jQuery? Can you share that part of your code? Commented May 23, 2017 at 7:09
  • @ricopo I'm using a laravel 5.4. So I've required jQuery in my bootstrap.js and in Vue mounted method is where I'm initializing the bootstrap-select. Commented May 23, 2017 at 7:18

3 Answers 3

1

I had similar issue and I fixed that by adding setTimeout like this:

setTimeout(function () {
   $('.select-reviewer').selectpicker();
}, 10);

or this one if you've already initialized selectpicker:

setTimeout(function () {
   $('.select-reviewer').selectpicker('refresh');
}, 10);

Also I should mention about selected option. Bootstrap-select removes any 'selected' attributes from when it's initialized or refreshed that's why here is the code which prevent this:

setTimeout(function () {
   $('.select-reviewer').selectpicker('val', $('.select-reviewer option:selected').val());
}, delay);

As you see from the code above we just set the value in bootstrap-select when it's initialised.

Sign up to request clarification or add additional context in comments.

Comments

1

have you tried using nextTick? init the bootstrapselect on the next tick of the vue clock

VUE docs, next-tick

Example

mounted: function() {
    this.getCurrentSeasonTeams().then( (response) => {
        if( response.status == 200 && typeof(response.data) == 'object' ) {
            this.nblTeams = response.data;
        }
    });

    this.$nextTick(function(){
        $('.selectteam').selectpicker();
    });
}

EDIT :

mounted: function() {
    this.getCurrentSeasonTeams().then( (response) => {
        if( response.status == 200 && typeof(response.data) == 'object' ) {
            this.nblTeams = response.data;

            this.$nextTick(function(){
              $('.selectteam').selectpicker();
            });
        }
    });
}

1 Comment

no console error @Taufik Akbar, I think I just solved it, thanks
0

not really sure if what I've made is a good solution for my issue. Added a setTimeout method and it works.

    this.getCurrentSeasonTeams().then( (response) => {
        if( response.status == 200 && typeof(response.data) == 'object' ) {
            this.nblTeams = response.data;
            if(typeof(this.nblTeams) == 'object'){
                setTimeout(() => {
                    $('.team').selectpicker();
                }, 500);
            }
        }
    });

3 Comments

@ricopo yes it needs the setTimeout to make it work, I think bootstrap-select checks the select box if it has options once the DOM is loaded, in my case maybe the loop is not yet finished rendering when I initialize bootstrap-select that's why select box has no options. Just a theory though
@PenAndPapers check my edit. your timeout method can fail if the list is longer
@TaufikAkbar yes I did, and I applied your solution, thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.