1
I have a select in a form, and the items in that select are dynamically populated by Vue.js.
My problem is that when I open a register saved in the database for editing on this form, I need to make some items selected according to what was saved before, and I don’t know how to do this.
If anyone knows another Vue.js-like library that is as light and easier to use, I also accept suggestions.
Just follow my code:
var setores = new Vue({
el: '#v-for-negociossetores',
data: {
setoresitens: {"countcats":16,"countsubcats":56,"catsubcatsdata":{"123":{"titulo":"Alimentos","descricao":"Padarias, Mercados, Restaurantes, Lanchonetes e etc.","subcats":{"345":{"codurl":"restaurantes","titulo":"Restaurantes","descricao":""}}},"abc":{"titulo":"Religiões","descricao":"","subcats":{"def":{"codurl":"igrejas","titulo":"Igrejas","descricao":""},"ghi":{"codurl":"religiao-casas","titulo":"Casas","descricao":""},"jkl":{"codurl":"religiao-templos","titulo":"Templos","descricao":""},"mno":{"codurl":"religiao-terreiros","titulo":"Terreiros","descricao":""}}}}}
}
});
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<div class="row clearfix">
<div class="col-sm-3">
<div class="form-group" id="v-for-negociossetores">
<select name="codsetores[]" class="form-control show-tick" multiple title="Setores">
<optgroup v-for="(group, keygroup) in setoresitens.catsubcatsdata" v-bind:label="group.titulo">
<option v-for="(option, keyoption) in group.subcats" v-bind:value="keyoption">
{{ option.titulo }}
</option>
</optgroup>
</select>
</div>
</div>
</div>
Our Samantha, it was very confusing that your question. If I understand what you want is popular select with the Database data is this?
– LeAndrade
Use as an example of the manual (second example for selects): https://vuejs.org/v2/guide/forms.html#Select
– bfavaretto
@Leandrade sorry, I do not know how to explain better in the question. But it is not that not, popular the select I already get that is what this in the question. But the select population comes from a json file. The q need is when open a registered record in the database, which in addition to popular select as I already do, that it already leaves selected the items that were previously saved. I don’t know if I can explain it right.
– Samanta Silva
@bfavaretto I looked at the documentation, that’s how I managed to get to where I posted. But I still don’t know how to leave some selected items. I didn’t see any similar example in the documentation, or at least I didn’t understand if there was something talking about it there. Can you help me with some more practical explanation? please
– Samanta Silva
You need an attribute
v-model=seumodelo
in the select tag. and in its objectdata
, oneseumodelo: valor_selecionado
. I think that’s it.– bfavaretto
@bfavaretto Ahh ok, I will try to understand more about the attribute v-model, thanks for the explanation, I will try here and Jaja I talk if it worked out, thank you.
– Samanta Silva
@bfavaretto Ahh worked, that’s right, obliged aa. Want to make a more detailed answer so I choose your answer?? Or do I answer myself and mention you in the answer?
– Samanta Silva