45

Here is my code:

JavaScript

let Names = [
    {
        Name: "Josh"
        FullName: ""
    },
    {
        Name: "Jonathan"
        FullName: null
    },
    {
        Name: "James"
        FullName: "James Johnson"
    }
];

Index.Vue

<ul>
    <li
        v-for="item in Names" 
        v-if=" item.FullName != null || item.FullName != '' "
    >
     {{FullName}}
    </li>
</ul>

This v-if=" item.FullName != null || item.FullName != '' " does not work, Why? How can I put two condition inside a v-if?

7
  • 1
    You should consider a computed property for this, by the way. Commented Nov 27, 2017 at 16:21
  • @ceejayoz That was my first thought, but it's actually inside a v-for so you couldn't really use a computed, but you could use a method. Commented Nov 27, 2017 at 16:25
  • Should i use computed or methods to this? I just want to not show li if FullName is '' (empty string) or null . Just that =x Commented Nov 27, 2017 at 16:26
  • 3
    Try v-if="!!item.FullName" Commented Nov 27, 2017 at 16:27
  • 1
    in javascript any empty,null or undefined values are falsy.. So, !! double negates and returns an actual boolean value representing if they are any of those falsy values or not. Commented Nov 27, 2017 at 16:30

5 Answers 5

65

Maybe it's the way you are treating empty strings as false-ly values and || is saying: show fullname if any of the two (left/right) expressions are true, which is not what you want I think.

Try this instead:

 <li v-for="item in Names" v-if="item.FullName !== null && item.FullName !== ''"> 

Also, judging from your code, {{ FullName }} should be {{ item.FullName }}

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

5 Comments

I have to use the operator ||, i tried to add one more =, but did not work :(
though the answer is right, it is a bad practice to use v-for and v-if on the same element. refer to Kamocsai Kamo András answer
@jedi rigth. this is stated in the documentation - vuejs.org/v2/style-guide/…
Is it just my eyes or is there an odd number of unmatched " in the answer above?
@MCCCS definitely not your eyes :)
10

Samayo's answer is right, and maybe this question a little old, but there are some issues:

First, please avoid v-for with v-if in the same element! (source: https://v2.vuejs.org/v2/style-guide/#Avoid-v-if-with-v-for-essential)

And second, if you want to use v-if with || operator, just use a computed, or simple method.

Finally, I think its the better way if you use camelCase, not the PascalCase for the variants or for object's keys.

<li v-if="fullNameIsExist(item)">
  <span>{{ item.fullName }}

And if you use a method:

methods: {
  fullNameIsExist (item) {
    if (![null, ''].includes(item.fullName)) return true
  }
}

Comments

2

The best way is using computed stuff

computed: {
    condition() {
      this.FullName;
      return this.FullName !== null && this.FullName !== '';
    }
  }

and is it as condition

<li v-for="item in Names" v-if="condition> 

and instead of {{ FullName }} you should use {{ item.FullName }} You were almost right with condition, but error was because of {{ FullName }} that's why you should write {{ item.FullName }} and because of || instead you better might use && Also you can do the stuff like this:

 <li v-for="item in Names" v-if="item.FullName !== null && item.FullName !== ''> 

1 Comment

You should not use v-for and v-if at the same time: vuejs.org/v2/guide/conditional.html#v-if-with-v-for
2

You can create a computed property that evaluates all of the conditionals then have a single v-if for your computed property. It will make your template cleaner and the computed property will be self documenting.

Comments

0

Simple and easy.

<li v-for="item in Names" v-if="!!item.FullName>

In Javascript, !!0 will return false, !!1 will return true, !!"" will return false, !!"a" will return trueand !!null will return false

1 Comment

don't use v-for and v-if together in Vue2. see documentation link in above answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.