1

in my Vue JS project i wanted to show data from API and its working good, my only problem is when i showed {{flat.status}} below in my code i wanted to play with CSS for ex: if status==Available let text color be green , and when status==Sold let text color be red and etc ..

is there a way to do it?

<b-th v-for="(flat, index) in Building.flats" :key="index" >
               <p> {{flat.status}} </p>
</b-th>

1 Answer 1

2

Yes! You can do this in a few ways, but the simplest is just to use conditional styles tags. In the :style, just write code!

<p :style=flat.status === 'available' ? 'color: green;' : 'color: 'red'>{{ flat.status }}</p>

If you'd like more CSS control, just use conditional classes.

<p :class="flat.status === 'available' ? 'text-green' : 'text-red'>{{ flat.status }}</p>

<style>
.text-green {
  // CSS here
}
</style>

There are other class binding styles you can look into for more information. They will give you other ways to do all this as well.

For multiple classes (similar example in the link I provided):

<div :class="{ 'text-green': flat.available, 'text-red': flat.sold, 'text-yellow': flat.booked }">{{ flat.name }}</div>
Sign up to request clarification or add additional context in comments.

4 Comments

I know this is should work perfectly as it is, but what if they had more than two status? What if we add more status after deployment ? what if the designer need to change the color ? I advice to add a new parameter color to their backend
thank you, but i have 3 status :D available :green , sold:red and booked in yellow .. is there a way to do it?
Yes! You can find that in the link I provided about class and style bindings, but I also added an example in the answer.
@sara97 Glad to have helped! I hope you continue to use and enjoy Vue, and be sure to post any more questions if needed!

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.