Created
September 1, 2019 22:19
-
-
Save joewashere/67e7e35aa05df6cd5aeabf8632f040d5 to your computer and use it in GitHub Desktop.
A simple component for product ratings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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