Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vue.js proper way to determine empty object

Tags:

vue.js

Classic scenario: I want to display a list, but when it's empty I want to display "No data".

The fact that it is somewhat complicated to do something I would expect to be simple makes me think I'm probably doing it wrong.

Here is my current solution.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <script src="http://cdn.jsdelivr.net/vue/1.0.16/vue.js"></script>    <div id="element">    <div v-if="empty">No item in inventory</div>    <div v-for="(index, item) in inventory">      {{item.key}}<button onclick="remove('{{index}}')">remove</button>    </div>  </div>    <script type="text/javascript">  "use strict";  var vm;  $(function() {      vm = new Vue({          el: '#element',          data: {              inventory: {"id1" : {"key" : "val1"}, "id2" : {"key" : "val2"}},              empty: false          },          watch: {              inventory: function() {                  vm.empty = $.isEmptyObject(vm.inventory);              }          }      });  });    function remove(key) {    Vue.delete(vm.inventory, key);  }  </script>

Is there a better solution than this?

like image 639
Finch_Powers Avatar asked Dec 16 '16 20:12

Finch_Powers


People also ask

How do I check if an object 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.

How do you check the object is empty or not in JavaScript?

keys method to check for an empty object. const empty = {}; Object. keys(empty). length === 0 && empty.

How do I check if a string is empty in Vue?

If the string is not undefined or null and if you want to check for empty string in Javascript we can use the length property of the string prototype as shown below. Or you can directly check for the empty string with javascript Comparison operator “===” as shown below. Thank you for reading !


1 Answers

You can just use length of inventory in v-if, like following:

<div id="element">   <div v-if="!inventory.length">No item in inventory</div>   <div v-for="(index, item) in inventory">       {{item.key}}       <button v-on:click="remove(index)">remove</button>   </div> </div> 

Given that inventory variable is a hash and not an array, you can use any of the following to find it is empty and use that in v-if:

ECMA 5+:

Object.keys(inventory).length === 0 

Pre-ECMA 5:

function isEmpty(obj) {     for(var prop in obj) {         if(obj.hasOwnProperty(prop))             return false;     }      return JSON.stringify(obj) === JSON.stringify({}); } 

As you are already using jquery, you can also do:

jQuery.isEmptyObject({}); // true 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="http://cdn.jsdelivr.net/vue/1.0.16/vue.js"></script>  <div id="element">    <div v-if="isEmpty">No item in inventory</div>   <div v-for="(index, item) in inventory">       {{item.key}}<button @click="remove(index)">remove</button>   </div> </div>  <script type="text/javascript"> "use strict"; var vm; $(function() {     vm = new Vue({         el: '#element',         data: {             inventory: {"id1" : {"key" : "val1"}, "id2" : {"key" : "val2"}},             empty: false         },         methods: {             remove: function(index) {                 Vue.delete(this.inventory, index);                              }         },         computed: {            isEmpty: function () {               return jQuery.isEmptyObject(this.inventory)            }        }     }); }); </script>
like image 101
Saurabh Avatar answered Oct 02 '22 21:10

Saurabh