0

my question is how can I show "show text" in the layout when "more": true is set in my JSON

My Layout:

<b-row v-for="?" v-if="?">
    <b-col>
        show text
    </b-col>
</b-row>

My JSON:

{
  "array": [
    {
      "id": "1",
      "more": false
    },
    {
      "id": "2",
      "more": true
    }
  ]
}
data () {
  return {
    n: 0,
    array: json
  }
}

EDIT: SOLVED IT => v-if="array[0].more"

7
  • 1
    Something like v-for="foo in array" v-if="foo.more" should do it, I think? If that doesn't work, try the v-if on the <b-col>. Commented Sep 16, 2018 at 21:55
  • @ceejayoz No :( Commented Sep 16, 2018 at 22:19
  • 1
    use a computed prop instead Commented Sep 16, 2018 at 23:07
  • @ceejayoz looks like it should be v-for="foo in array.array". It's an odd structure Commented Sep 17, 2018 at 0:50
  • @Phil is there a better way? Commented Sep 17, 2018 at 6:17

2 Answers 2

2

I've never been a fan of conditional rendering in a loop.

My preference is to use a computed property that returns a filtered set of your iterable object. For example

computed: {
  more () {
    return this.array.array.filter(({ more }) => more)
  }
}

then in your template

<b-row v-for="item in more">
  <b-col>
    show text
  </b-col>
</b-row>
Sign up to request clarification or add additional context in comments.

Comments

1
<b-row v-for="el of array.array" v-if="el.more">
    <b-col>
        show text
    </b-col>
</b-row>

In your json, you have your array in the property array. Your vue component set the data of the json in the property array. This means the array is accessible in array.array.

1 Comment

v-for="array of/in array.array" v-if="array. more" doesn't work. it shows the row both when it`s true and false.

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.