0

I want to show a button if my array of object has some data, so basilcy in my store(vuex) i defined a array like this:

state: {
    document: []
},

i append data to this array from other components and i already checked that the data is appending right, no problem here.

So i want to show the button just if there is some data:

<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>

there is the button, i want to hide the whole div with the button if the condition matches, but it is not working the button is always there, any help?

5
  • why you use single quotations in this.$store.getters.getDocument != '[]'? Isn't document an array? Commented Aug 2, 2017 at 11:24
  • i tried without it either, same result Commented Aug 2, 2017 at 11:26
  • what if you do v-if="document.length > 0" ? Commented Aug 2, 2017 at 11:29
  • @FilipeCosta Yeah, if your document is an array, you shouldn't use "!=", see: stackoverflow.com/questions/40313263/why-is-in-javascript Commented Aug 2, 2017 at 11:37
  • makes sense, im sleeping :) Commented Aug 2, 2017 at 11:41

2 Answers 2

3

Check its length property.

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

Or assign the vuex variable to null when there are no elements. Than this should work.

<div v-if="this.$store.getters.getDocument">
    <button class="btn btn-success btn-block">Create Document</button>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

worked, dunno i had tried something like that, thank you friend :D
1

In your store did u define the getter "getDocument", if so add a computed property in your component, it much cleaner and more reusable then referencing the store getters in the template directly:

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.