Vuetify is a popular UI framework for Vue apps.
In this article, we’ll look at how to work with the Vuetify framework.
Ratings
The v-rating component lets us add a star rating input to our app.
To use it, we can write:
<template>
<div>
<v-rating v-model="rating" background-color="purple lighten-3" color="purple" small></v-rating>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
rating: 4,
}),
};
</script>
The v-model has the rating value.
background-color has the fill color.
color has the outline color.
small makes the stars extra small.
We can also set the size to large , x-large , and the size prop.
The size prop lets us set the stars to any size value in pixels.
Custom Length
We can change the number of stars to display.
To do that, we use the length prop:
<template>
<div>
<v-rating v-model="rating" background-color="purple lighten-3" color="purple" :length="10"></v-rating>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
rating: 4,
}),
};
</script>
We set length to 10, so we see 10 stars displayed.
Incremented
We can add full icons, half icons, or empty icons.
For example, we can write:
<template>
<div>
<v-rating v-model="rating" background-color="purple lighten-3" color="purple" half-increments></v-rating>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
rating: 4.5,
}),
};
</script>
The half-increments prop lets us add half stars.
So when we set rating to 4.5, we’ll see 4.5 stars.
Slots
We can populate the star slots to customize the stars as we wish.
For example, we can write:
<template>
<div class="text-center">
<v-rating v-model="rating">
<template v-slot:item="props">
<v-icon
:color="props.isFilled ? genColor(props.index) : 'grey lighten-1'"
large
@click="props.click"
>{{ props.isFilled ? 'mdi-star-circle' : 'mdi-circle-outline' }}</v-icon>
</template>
</v-rating>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
colors: ["green", "purple", "orange", "indigo", "red"],
rating: 4.5,
}),
methods: {
genColor(i) {
return this.colors[i];
},
},
};
</script>
We have the isFilled property from the slot props to determine whether the star should be filled or not.
The click method can be passed into the @click directive as the click handler.
This way, when we click it, we’ll see the value displayed with the stars.
We have different colors from different stars by returning the color name from the genColor method.
Card Ratings
We can put a v-rating component on a card.
For example, we can write:
<template>
<v-card class="mx-auto elevation-20" color="purple" dark style="max-width: 400px;">
<v-row justify="space-between">
<v-col cols="8">
<v-card-title>
<div>
<div class="headline">Song</div>
</div>
</v-card-title>
</v-col>
<v-col cols="4">
<v-img
class="shrink ma-2"
contain
height="125px"
src="https://randomuser.me/api/portraits/women/68.jpg"
style="flex-basis: 125px"
></v-img>
</v-col>
</v-row>
<v-divider dark></v-divider>
<v-card-actions class="pa-4">
Rate this album
<v-spacer></v-spacer>
<span class="grey--text text--lighten-2 caption mr-2">({{ rating }})</span>
<v-rating
v-model="rating"
background-color="white"
color="yellow accent-4"
dense
half-increments
hover
size="18"
></v-rating>
</v-card-actions>
</v-card>
</template>
<script>
export default {
name: "HelloWorld",
data: () => ({
rating: 4.5,
}),
methods: {
genColor(i) {
return this.colors[i];
},
},
};
</script>
We put the v-rating component in the v-card-actions to make it show at the bottom of the card.
Conclusion
We can add the v-rating component to let us add a star rating input to our app.