In my third exercise, I have a json file that contains info about cars and their models. I am trying to populate 2 dropdowns from that file:1 for cars and the corresponding models in the other dropdown but not able to do that.
In the models dropdown that I did below, i see some null elements! (maybe corresponding of a place occupied in previos selection). I would like to have the models dropdown always have the first item by default. Thanks for help.
json file data:
[
{"name": "Abarth",
"models": [
{"name": "124 Spider", "series": null},
{"name": "500", "series": null},
]
},
{"name": "Acura",
"models": [
{"name": "MDX", "series": null},
{"name": "NSX", "series": null},
{"name": "RL", "series": null},
{"name": "RSX", "series": null},
]
},
]
My Code:
<template>
<div>
Make:<select @change="switchView($event, $event.target.selectedIndex)">
<option
v-for="(item, index) in logitems"
:key="index"
v-bind:value="this.selected"
>
{{ item.name }}
</option>
</select>
Model:<select>
<option
v-for="(item, index1) in logitems"
:key="item"
>
{{ logitems[selectedIndex].models[index1]}} //not able to get the name !
</option>
</select>
</div>
</template>
<script>
import logs from "../assets/car-makes.json";
export default {
data() {
return {
logitems: logs,
selectedIndex: "0",
selected: {
name: "Abarth",
models: [
{
name: "124 Spider",
series: null,
},
{
name: "500",
series: null,
},
],
},
};
},
methods: {
switchView: function (event, selectedIndex) {
console.log(event, selectedIndex);
this.selectedIndex = selectedIndex;
},
},
};
</script>