Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A complex condition inside v-if

Tags:

vue.js

I want to create a complex condition to pass to the v-if directive.

I have tried the following.

<div v-if="(order.order_products[key].statuses[0].id) != 3 || order.status != 3" >

Can I add a complex condition in Vue's v-if? This is not working.

I also tried with && but that wasn't working, either. I haven't found anything in the documentation about this.

like image 531
wenus Avatar asked Aug 27 '17 15:08

wenus


People also ask

What does V-if do in Vue?

v-if. The directive v-if is used to conditionally render a block. The block will only be rendered if the directive's expression returns a truthy value.

What is the difference between V-if and V-show?

The key difference is that v-if conditionally renders elements and v-show **conditionally displays **elements. This means that v-if will actually destroy and recreate elements when the conditional is toggled. Meanwhile, v-show will always keep the element in the DOM and will only toggle its display by changing its CSS.

How do you write if in Vue?

The v-if directive in Vue-js computes a condition to be true into the DOM. When the condition shows false it completely removes the element from the DOM. The if-condition is used in cases where the form element needs to show an error when the user has no input in the input field.

What is Vshow?

The v-show directive is a Vue. js directive used to toggle the display CSS property of a element with our data via inline styles. If the data is true it will make it visible else it will make it invisible.


3 Answers

Firstly, to answer your question.

Can I add a complex condition in Vue's v-if?

You can pass an arbitrary JavaScript expression to the v-if directive in Vue, including a complex boolean expression which contains operators || or &&.

You can test this on your own. For example, try having the following template.

<div v-if="true && false">I am not visible!</div>

Of course, you might try out something less trivial, too:

<div v-if="1 == 2 || (1 + 2 == 3 && 4 == 4)">I am visible!</div>

Your expression looks good, but based on the provided information it's impossible to deduce what exactly is wrong.

Your problem is something else: maybe the data is not in the format you thought it would be, or maybe your logic has a flaw in it.

like image 155
Lazar Ljubenović Avatar answered Oct 24 '22 06:10

Lazar Ljubenović


Yes, you can set complex condition. I suggest you to use Vue computed fields, so you will have better highlight (through Vue Devtools) of variables which use in v-if expression. I suppose that order is data field, so you can set computed fields like this:

computed: {
    orderStatusId: function () {
        // Write some checks is variable defined/undefined
        return this.order.order_products[key].statuses[0].id
    },
    orderStatus: function () {
        return this.order.status
    }
}

and v-if expression should look like this:

<div v-if="orderStatusId !== 3 || orderStatus !== 3" >

In this approach you can review values of variables in your v-if expression.

like image 32
poletn23 Avatar answered Oct 24 '22 05:10

poletn23


Yes, you can use any JavaScript expresion inside v-if quotes. But I recommend you to create a function or computed function and to call it inside your if statement, for better readability.

ex:

computed: {
  shouldDisplay: function () {
    return this.order.order_products[key].statuses[0].id) !== 3 || this.order.status !== 3;
  }
  ...
}

<div v-if="shouldDisplay"> ... </div>
like image 4
V. Sambor Avatar answered Oct 24 '22 07:10

V. Sambor