1

I'm facing an issue with adding dynamic styling to an element based on it's contents.

<td class="text-xs-left">{{ props.item.limitCardStatus }}</td>

The values of the limitCardStatus key are stored in several objects, and are as follows: "Active, Inactive, Sample".

What i want to achieve is to color "Active" green, "Inactive" red, and "Sample" yellow.

What i've have so far is: I generated the coresponding CSS classes & styles, and i tried using v-for with :class, but i can't seem to figure it out.

Here's the full table:

<v-data-table :headers="headers" :items="limitCards" hide-actions>
 <template v-slot:items="props">
  <td class="tableNumber text-xs-left">{{ props.item.number }}</td>
  <td class="text-xs-left">{{ props.item.limitCardNumber }}</td>
  <td class="text-xs-left">{{ props.item.productCode }}</td>
  <td class="text-xs-left">{{ props.item.plannedAmount }}</td>
  <td class="text-xs-left">{{ props.item.productName }}</td>
  <td
     class="text-xs-left"
     :class="{
     'green--text': limitCardStatus === 'Active',
     'yellow--text': limitCardStatus === 'Inactive',
     'yellow--text': limitCardStatus === 'Sample',
     }"
     >{{ props.item.limitCardStatus }}</td>
  </template>
 </v-data-table>

Can someone point me in the right direction? Thanks in advance!

2 Answers 2

1

You can do it easily with the dynamic classes like so

<div
  class="text-xs-left"
  :class="{
    'green-color--text': limitCardStatus === 'Active',
    'yellow-color--text': limitCardStatus === 'Inactive',
    'yellow-color--text': limitCardStatus === 'Sample',
  }"
>{{ props.item.limitCardStatus }}</div>

More information is here

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

4 Comments

Thank you sir, that's wonderful! I will accept your answer in 5 minutes, because it wont let me at the moment.
Hmm, i've tried your apporach, but i'm getting this error: Property or method "limitCardStatus" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. At the moment, the limitCardStatus data is stored in hardcoded array of objects, and i've tried reffering it by the array itself, and with this., but nothing worked. Any ideas?
@Pok3rPrinc3 all you need is to replace my/your variable limitCardStatus to the one where your status is. Mb it has to by something like props.item.limitCardStatus
Yeah, i've tried that at some point aswell :) The problem was: 'yellow-color--text' is actually yellow--text. Still your answer was the right one!
1

Cleaner approach is to use ES6 computed property names

Assuming you're passing in prop named limitCardStatus to the component,

  <td
     class="text-xs-left"
     :class="{ [limitCardStatus]: 1 }"
     >{{ props.item.limitCardStatus }}</td>

1 is just there because there must be a truthy value on the right side for vue to apply the class name, you could do {{ [limitCardStatus]: limitCardStatus }} if you're not always passing in limitCardStatus prop to the component.

After, you can just replace CSS selectors for .green-text etc... with .Active .Inactive and .Sample

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.