0

So I have a list of data brought in using vue resource from an api. I'm trying to integrate this list with bootstrap accordion.

So:

I have set this :

data: {
  taller: false
}

and

<div class="panel panel-default"
  v-repeat="faq: faqs | filterBy searchText" 
  v-transition="staggered" 
  stagger="200"
  v-on="click: toggleHeight"
  v-class="active: taller">

So on click i'll call toggleHeight and pass through the faq instance :

toggleHeight: function(faq) {
  this.taller = true;
}

This function sets to taller to true however it sets it to true for all faq items, not just the one i've passed to toggle height.

How can I only return taller: true for clicked faq item?

Thanks

2 Answers 2

2
<div id="demo">

        <div class="input-group">
            <input class="form-control" v-model="searchText">
        </div>

        <script id="item-template" type="x-template">


            <div 
            class="stag"
            v-on="click: toggleHeight()"
            v-class="active: taller"
            >

                <h3>{{  item.question }}</h3>

                <h4>{{  item.answer }}</h4>

            </div>

        </script>

        <question 
            v-repeat="item: items | filterBy searchText" 
            v-transition="staggered" 
            stagger="200">
        </question>

    </div>
</div>

and the js:

Vue.component('question', {

        template: document.querySelector('#item-template'),

        data: function() {

            return {

                taller: false

            }

        },

        methods: {

            toggleHeight: function() {

                this.taller = ! this.taller

            }
        }

    });

    new Vue({

        el: '#demo',

        ready: function() {

            this.fetchItems();

        },

        methods: {

            fetchItems: function() {

                this.$http.get('/dev/frequently-asked-questions/api/index', function(items) {
                    this.$set('items', items);
                });

            }

        }

    });

Had to make use of components to target each item more directly.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you only have one variable taller. Not one per faq. So you'd have to change it a bit to something like this:

new Vue({
    el: '#faq-list',
    data: {
        faqs: [{id:'foo',taller:false},{id:'bar',taller:true}]
    },
    methods: {
        toggleHeight: function (faq, ev) {
            faq.taller = false;
            ev.target.classList.add('active');
        }
    }
});

And your HTML to something like this:

<div id="faq-list">
<div class="panel panel-default"
  v-repeat="faq: faqs" 
  v-on="click: toggleHeight (faq, $event)"
  v-class="active: taller">
      {{ faq.id }}</div>
</div>

And for fun, I added CSS to see things working:

.active {
    color: red;
}

Here's the JSfiddle, for your reference and to mess around with it. Click on one of the items in the list and it turns red.

5 Comments

Hey, thanks for your answer. Problem I have is that data: { } is brought through via an api, and captured using vue resource. I can't append taller to each data item.
Then that item is effectively singular and/or global. It doesn't relate to individual data points. There's only one of them. When you get it from an API, do you have a "taller" attribute for each FAQ? Or is it intended to be just one variable?
I do not have the taller attribute for each FAQ, it is intended to be just one variable.
Then I'm confused why you would think you'd be able to change it for just one faq. It would apply to all.
I've sorted it, please see new answer, thanks for your help though.

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.