Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Condition on value for style on Vue.js

I'd like to check the value of my template

        <template slot="projected_end_date" slot-scope="{ line }">
            <datetime-display :value="line.projected_end_date"
                              type="date"
            style= "color: red"></datetime-display>
         </template>

and only set the style for red color when my value is less than current date. Any suggestion? I'm assuming it should be something like

value > DateHelper.now()? style = ...
like image 776
Nicky Mirfallah Avatar asked Oct 27 '25 13:10

Nicky Mirfallah


1 Answers

You can use class and style binding like this:

:style="value < DateHelper.now() ? { 'color': 'red'} : ''"

Or,

:style="{color: value < DateHelper.now() ? 'red' : 'inherit'}"

Or some default color:

:style="{color: value < DateHelper.now() ? 'red' : 'black'}"

But I suggest you to use class binding whenever possible:

:class="{warning: value < DateHelper.now()}"

And in your css styles:

.warning {
   color: red;
}

Also, you can simply use Date.now() instead of a helper function.

like image 122
Bhojendra Rauniyar Avatar answered Oct 29 '25 02:10

Bhojendra Rauniyar