Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if list is empty inside a vue.js template?

Pretty simple question.

In my vue.js template I have:

<template v-for="node in nodes">
  [[ node ]]
</template>

I want to return nodes in the list or print "N/A" if the list is empty.

I know how to do this with a js function, but not sure how to accomplish the same thing from within the template.

like image 767
david Avatar asked Jun 05 '19 05:06

david


People also ask

How do I check if an array is empty in Vue?

If you are a using core jquery and you need to check array or object is empty or not then you can do it easily. In vue js you can't do it very simple, but you can do it using v-if and array. length.

What is ref() in vue?

Refs are Vue. js instance properties that are used to register or indicate a reference to HTML elements or child elements in the template of your application. If a ref attribute is added to an HTML element in your Vue template, you'll then be able to reference that element or even a child element in your Vue instance.

How do you empty an object in Vue?

Conclusion. To delete a property from a data object with Vue. js, we can use the this. $delete method.

What does template tag do in Vue?

Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data.

How to check if an array is empty in Vue JS?

If you are a using core jquery and you need to check array or object is empty or not then you can do it easily. In vue js you can't do it very simple, but you can do it using v-if and array.length. Read Also: How to use setTimeout in Vue JS?

How to check if variable is empty or null with Vue v-if?

To check if variable is empty or null with Vue v-if and JavaScript. we can set v-if to the variable we want to check. to add the Vue script and the app container. In the mounted hook, we set this.archiveNote to 'note' which will make it truthy in the setTimeout callback which will run in 1 second after mounted is run.

How to check if a list is empty or not?

to check if a list is empty, you just need to use v-if, in that way your template will be like below : Show activity on this post.

How to check if node is undefined in Vue JS?

You should check for both undefined and length using Vue's v-if. If you ever use asyncComputed for example, it will be undefined until the async method resolves. So you are covered for nodes=null, nodes=undefined, and nodes= [].


2 Answers

You should check for both undefined and length using Vue's v-if. If you ever use asyncComputed for example, it will be undefined until the async method resolves. So you are covered for nodes=null, nodes=undefined, and nodes=[].

<template v-if="!nodes">
  Loading
</template>
<template v-else-if="!nodes.length">
  Empty
</template>
<template v-else v-for="node in nodes">
  {{ node }}
</template>

Or combine them

<template v-if="!nodes || !nodes.length">
  Nothing To Show
</template>
<template v-else v-for="node in nodes">
  {{ node }}
</template>

I like using !nodes.length instead of length !== 0 just in case something unexpected besides an array ends up in the data, in which case it will most likely use the empty template and not throw an exception. Less typing too!

like image 195
Steven Spungin Avatar answered Sep 24 '22 07:09

Steven Spungin


You can check like:

<template v-if="nodes.length">  // When there are some records available in nodes
  // Iterate it and display the data
</template >
<template v-else>
  // No record found
</template>

Reference

like image 30
Mayank Pandeyz Avatar answered Sep 23 '22 07:09

Mayank Pandeyz