2

I have an array- selected_players, which I am looping through in Vue.js, but am not able to set the selected attribute of an option.

What I am trying: :selected="player.round.best_player == 1"

Here is the section of the related template:

    <div v-if="selected_players.length && ! loading">
        <h4>Select Best Player</h4>
        <div class="form-group">
            <select name="best-player" id="best-player" v-model="best_player" class="form-control">
                <option v-for="player in selected_players" :value="player.id" :selected="player.round.best_player == 1">{{ player.name }}</option>
            </select>
        </div>
        <br />

When loaded, this is the related HTML:

<select name="best-player" id="best-player" class="form-control">
    <option value="1">Ashley</option>
</select>

How can I achieve this?

2
  • What happens when you change your selected="player.round.best_player == 1" statement as selected="{{player.round.best_player == 1}}" ? Commented Jan 16, 2017 at 7:06
  • Thank you for the comment, but from memory, Vue.js 2.x only allows setting properties programmatically via binding. Commented Jan 16, 2017 at 7:32

1 Answer 1

2

To have a selected option, you can just set the best_player as the one you want to have by default selected.

With below code I am iterating over your player array and finding the player which has round.best_player === 1. The idea is to set the best_player with the selected option.

best_player = player.filter(p => p.round.best_player === 1).id
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was the fix :)

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.