Skip to content

Instantly share code, notes, and snippets.

@joewashere
Created September 1, 2019 22:19
Show Gist options
  • Save joewashere/67e7e35aa05df6cd5aeabf8632f040d5 to your computer and use it in GitHub Desktop.
Save joewashere/67e7e35aa05df6cd5aeabf8632f040d5 to your computer and use it in GitHub Desktop.
A simple component for product ratings
// Product Star Rating Display
Vue.component("prod-rating", {
props: ["rating"],
data(){
return {
stars: []
}
},
mounted: function () {
let ratingTracker = this.rating;
// Loop 5 times for 5 stars!
for(i=1; i<=5; i++){
// Is the rating higher than our iterator?
if(this.rating >= i){
// Full star for you!
this.stars.push("icon-star-full");
ratingTracker--;
}else{
// No? How about we see if we have anything left...
if(ratingTracker % 1 !== 0){
// Half a star for you!
this.stars.push("icon-star-half");
ratingTracker = 0;
}else{
// Nothings left. No stars for you!
this.stars.push("icon-star-empty");
ratingTracker--;
}
}
}
},
template: `
<div class="rating">
<span v-for="star in stars" :class="star + ' icon glyph'"></span>
</div>
`
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment