0

My select box is working fine when the variable item.ativo_retencao.id exists correctly.

<select name="ativo_retencao" v-model="item.ativo_retencao.id" class="form-control m-input">
  <option v-for="ativo in ativos" v-bind:value="ativo.id">@{{ ativo.title }}</option>
</select>

So when it does not exists, I want to not define (or set null) the v-model property.

Is that possible?

2
  • And what behavior would it be when you can select an option and the selected option will not be saved at all? Commented Nov 4, 2020 at 17:16
  • @Anatoly Good question. It does not make sense to render select at all in this case...just use v-if Commented Nov 4, 2020 at 17:33

2 Answers 2

1

you can use an ternary to do this:

<select name="ativo_retencao" :v-model="item.ativo_retencao.id ? item.ativo_retencao.id : ''" class="form-control m-input">
  <option v-for="ativo in ativos" v-bind:value="ativo.id">@{{ ativo.title }}</option>
</select>
Sign up to request clarification or add additional context in comments.

3 Comments

it's causing SyntaxError: Unexpected token ','
I don't think so ....null is not valid "left side" for assigment
and ''' is not either...try again
1

v-model is just syntactic sugar (code below applies just for select element):

v-model="item.ativo_retencao.id"

..is same as

:value="item.ativo_retencao.id" v-on:change="item.ativo_retencao.id = $event.target.value"

...so if you don't have a value you want to bind to, just don't render the select at all

<select v-if="item.ativo_retencao.id" name="ativo_retencao" :v-model="item.ativo_retencao.id" class="form-control m-input">
  <option v-for="ativo in ativos" v-bind:value="ativo.id">@{{ ativo.title }}</option>
</select>

Comments

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.