I have a json which I have kept inside my Vue data as shown below:
new Vue({
el: 'myDiv',
data: {
dishes: Dishes
}
})
And my Json as below:
Dishes = [
{
Id: 1,
Name: "First dish",
Rating: 2.5
},
{
Id: 2,
Name: "Second dish",
Rating: 1.5
},
{
Id: 1,
Name: "Third dish",
Rating: 4.5
}
]
Which I use in my DOM as shown below:
<ol id="myDiv">
<li v-for="dish in dishes">
{{dish.Name}}
//call a function to adjust ratings and get the generated html,
//eg:call function ratingAdjustor(dish.Rating);
<li>
</ol>
My function ratings adjustor is as shown below:
function ratingAdjustor(rating){
if(rating % 1 != 1){
//round to the nearest value
//maybe nearst value is 3
var stars = "";
for(i=0; i< roundedRaating; i++){
stars += '<span>//Star SVG icons highlighted</span>';
// hence 3 highlighted stars
}
//rest of the non highlighted stars
return stars; // which has stars based on ratings
}
}
Since data manipulation is in dom in 'v-for' I have no idea how to proceed, I want to take rating value from v-for, call a function which gets the nearest value and creates html elements with stars and returns string containing html. But I can't figure out how to do that. 'v-on' helps only if there is any event eg: 'v-on:click="someFunctionName"', since this should happen while running loop not on event, it is getting tricky.
object- then it's a Javascript object - or it's JSON - then it's astring(that is formatted following certain conventions which together define the JSON format).