1

I have this for loop in my asp.net page, then in the same site I have to do the same but in vue.js, I'm not familiar with vue I've check the v-for but I can't manage to get the same result, can someone show me the right way please?

This is my for loop in asp.net, I just need to translate this in vue.js :

<div>
@for (i = 1; i <= 5; i++)
{
if (i <= (rating))
{
    <i class="icon-ned icon-ned-star red"></i>
}
else
{
    <i class="icon-ned icon-ned-star gray"></i>
}
}
</div>

Any ideas ?

2 Answers 2

3

Did you checked vue documentation, its well written.

https://v2.vuejs.org/v2/guide/list.html#v-for-with-a-Range

<div>
  <i 
   v-for="n in 5"
   class="icon-ned icon-ned-start"
   :class="{
     'red': n <= rating,
     'gray': n > rating
   }"
  ></i>
</div>

or :class="n <= rating ? 'red' : 'gray'" for classes list with toggling

Sign up to request clarification or add additional context in comments.

6 Comments

OP also wants to add grey and red icons based on rating
Edited answer with icons and colors
ok I see, the thing is that it's a 5 stars rating, so if I have 4/5 the last star should be other color
I must have something wrong in the syntaxe, I'm getting this in the page <= rating ? 'red' : '', n > rating ? 'gray' : ''] > do you know why?
You don't need to use array syntax there, use previously mentioned :class="n <= rating ? 'red' : 'gray'". Of course be sure if "rating" is defined for you
|
0

I just ran into this problem and fixed it like you see below;

Please Note that I am using google material icons for this purpose and tailwind for the styling. but either way this will still work with plain CSS just make sure you declare the classes you are aiming at correctly;

Here you go:

<span 
 v-for="n in 5"
 :key="n"
 :class=" 
   n <= rating 
     ? 'text-yellow-400 material-icons' 
     : 'text-gray-300 material-icons'
    ">
 star 
 </span>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.