Verify whether the JSON array is devoid of any elements within the Vuex store

If my array of objects contains data, I want to display a button. In my Vuex store, I have defined an array like this:

state: {
    document: []
},

I am adding data to this array from other components and I have confirmed that the data is being added correctly with no issues.

So the goal is to only show the button if there is data present:

<div class="row margin-above">
    <div class="panel panel-primary" v-for="section in this.$store.getters.getDocument">
        <div class="panel-body quote" >
            <p>{{section.key}}</p>
        </div>
    </div>
    <div v-if="this.$store.getters.getDocument != '[]'">
        <button class="btn btn-success btn-block">Create Document</button>
    </div>
</div>

The issue I'm facing is that the button always appears even when the condition should hide it. Any suggestions?

Answer №1

Make sure to check the length property of the element.

<div v-if="this.$store.getters.getDocument.length != 0">
    <button class="btn btn-success btn-block">Click Here to Create Document</button>
</div>

Alternatively, you can set the vuex variable to null if there are no elements. This approach should also work.

<div v-if="this.$store.getters.getDocument">
    <button class="btn btn-success btn-block">Click Here to Create Document</button>
</div>

Answer №2

Have you implemented the "getDocument" getter in your store? If so, consider adding a computed property to your component instead of directly referencing the store getters in the template. This approach is cleaner and more reusable:

computed: {
   document: function() { 
       return this.$store.getters.getDocument; 
   }
}

In the template:

<div v-if="document.length">
    <button class="btn btn-success btn-block">Create Document</button>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm looking to generate a semicircle progress bar using jQuery, any suggestions on how

Hi there! I'm looking to create a unique half circle design similar to the one showcased in this fiddle. Additionally, I want the progress bar to be displayed in a vibrant green color. I've recently started learning about Jquery and would apprec ...

Using the Angular JSON pipe with angular-datatables

I am currently utilizing angular-datatables to exhibit NoSQL de-normalized data in a grid format for visualization purposes. Within my dataset, I have several intricate nested JSON objects and I intend to showcase a specific cell with formatted JSON using ...

JsonMappingException: Unable to find a valid constructor for the specified type [simple type, class car.Car$Parts]

My current task involves deserializing this XML data and converting it into a Parts object: <Parts> <Part> <Name>suspension</Name> <Year>2010</Year> </Part> <Part> ...

Vuejs method to showcase input fields based on form names

I am working on a Vue.js form component with multiple input fields, but now I need to split it into two separate forms that will collect different user input. Form 1 includes: Name Surname Email with a form name attribute value of form_1 Form 2 i ...

Automatically bring in functions in JavaScript without explicitly declaring them

I am working with 2 files. //main.js const one = (text) => { return text; } const two = (text) => { return text + " is here"; } module.exports = [ one, two ] //app.js const data = require("./main.js"); console.log(data.one("exampl ...

How to retrieve a row from the database in JSON format in Laravel when the data in one column is stored as JSON

I am facing an issue with retrieving JSON data from my database. The column where I store the data is supposed to hold JSON format, but when I retrieve it as a response in PHP, it shows up as a string instead of a JSON object. Here is the structure of my ...

What could be causing my right-floating elements to shift more towards the left with each occurrence?

After following a todo list tutorial, everything was running smoothly until I decided to add some styling to it. I placed a Font Awesome trash can icon inside a button with the class "remove." Essentially, I removed all the styles from the button, leaving ...

Jquery attribute not functioning correctly when setting the 'required' property for checkboxes

One of the challenges I am facing is trying to toggle a text box on a checkbox's click event. While I have achieved success in this aspect, my issue lies in changing the checkbox's required attribute as well. Below is the code that successfully ...

Sharing data between a Javascript applet and a webpage: strategies for sending results from the applet back to

When it comes to calling a method in an applet from JavaScript, I am facing a challenge. Both the applet and the JavaScript code are running on the same webpage. I am aware of how to call applet methods from JavaScript and vice versa using techniques like ...

Can inner function calls be mimicked?

Consider this scenario where a module is defined as follows: // utils.ts function innerFunction() { return 28; } function testing() { return innerFunction(); } export {testing} To write a unit test for the testing function and mock the return value ...

Parsing a diverse JSON array into a variant List<> with the help of Json.NET

I am faced with a JSON-array that holds objects of various types with different properties. The key property, "type," determines the nature of each object within the array. Take a look at an example snippet of my data below: [{ type : "comment" ...

Error: Unable to access the currentTime property as it is not defined

Incorporating Videojs into my react application has presented a challenge. I am attempting to set the current time of the videojs player, but keep encountering an error that reads "cannot read property currentTime of undefined". Below is my approach: var ...

Creating an instance of a child class using a parent class variable

I am facing an issue with passing a variable from my parent component to the child component. Despite trying multiple solutions, I have not been able to make it work. Here is the code I have: Parent : <template> <Widget/> </template&g ...

Error: Attempting to destructure the 'recipes' property from 'props' results in a TypeError due to its undefined value

I am new to working with reactjs and encountered an error when trying to run a program. The specific error message is as follows: Uncaught TypeError: Cannot destructure property 'recipes' of 'props' as it is undefined. at RecipeList ...

Exploring: Accessing the Tags Attribute

For a project, I am integrating wicket and jQuery together. In my HTML, I have: <a wicket:id="link" testAttr="test"></a> With jQuery, I am updating this attribute when other components on the page are clicked. My question is: how can I retri ...

In Angular, is there a way to concatenate a variable with "blah" at the beginning and end?

Success! Check out this snippet: <div ng-include="'/partials/' + items.my_filename + '.html'"></div> Despite finding the file, an error is still thrown: GET http://localhost:9000/partials/.html 404 (Not Found) Any ideas ...

Retrieve JSON data from a WebApi controller by utilizing the Included properties

I'm looking to modify my Web Api 2 controller to return JSON, but I've encountered an issue with Included properties. Here is my current get method: [System.Web.Mvc.HttpGet] public IQueryable<Employee> GetEmployee() { return db.Employe ...

I am implementing a new method in the prototype string, but I am uncertain about its purpose

I am trying to wrap my head around the concept here. It seems like the phrase will pass a part of an array, in this case eve, to the phrase.palindrome method. This method will then process it. First, the var len takes the length of eve and subtracts 1 from ...

When trying to generate a list in Vue.js using Vuetify, an error occurred stating that it is best to steer clear of using a JavaScript keyword such as "v

Attempting to generate a basic list in Vue.js using Vuetify, I encountered an error message: Avoid using JavaScript keyword as "v-on" value: "" vue/valid-v-on. Can someone provide a detailed response, considering my limited knowledge of Vuetify and Vue.js? ...

The process of making a pop-up modal instead of just relying on alerts

Attempting to change from using an alert to a pop-up with a simple if statement, but encountering some issues. Here is the current code: if(values == ''){ $('body').css('cursor','auto'); alert("Blah Blah..." ...