I have a list of cars as below:
<script>
const sampleCars = [
{
id: 1,
name: 'Cressida',
model: 'XXC',
manufacturer: 'Toyota',
price: '$10,000',
inEditMode: false
},
{
id: 2,
name: 'Corolla',
model: 'ZD-2',
manufacturer: 'Toyota',
price: '$12,000',
inEditMode: false
},
{
id: 3,
name: 'Condor',
model: '27-9',
manufacturer: 'Mazda',
price: '$8,000',
inEditMode: false
}
]
export default {
data() {
return {
cars: sampleCars
}
}
}
</script>
<style>
.grid {
display: grid;
grid-template-columns: repeat(5, auto);
gap: 10px;
}
</style>
I want to display the items in a css grid with 5 columns.
If I use vue code like below:
<template>
<div >
<div class="grid">
<div>Name</div>
<div>Model</div>
<div>Manufacturer</div>
<div>Price</div>
<div>buttons</div>
</div>
<div v-for="car in cars" :key="car.id" class="grid">
<div>{{car.name}}</div> // "child div"
<div>{{car.model}}</div>
<div>{{car.manufacturer}}</div>
<div>{{car.price}}</div>
<div>buttons</div>
</div>
</div>
</template>
The problem with this code is that each item is displayed in its own grid. (And therefore not aligned as in image below). Using v-for all car properties become a child of the root div. So essentially I want all "child divs" to be a root of one CSS grid div. How can I achieve that?
With VueJS 2