1

I create a array which contains objects describing information of buttons. In my template I have a class active which is represent the active button. By default the first button of the array is active.

template:

<b-button-group class="page-buttons mt-2" size="sm">
   <b-button v-for="btn in buttons" 
      :key="btn.key" 
      @click="selectPage(btn.key)"
      v-bind:class="{'active': btn.active}">{{ btn.caption }}
   </b-button>
 </b-button-group>

Script:

methods: {
    $createPaginationButtons: function(numberParameters) {
      const numberPages = Math.ceil(numberParameters / NUMBER_ELEMENT_PER_PAGE);
      this.buttons = [];

      for (let i = 0; i < numberPages; i++) {
        this.buttons.push({
          caption: `PAGE ${i + 1}`,
          active: (i === 0),
          key: (i + 1)
        });
      }

      Vue.set(this.buttons[0], 'active', true);
   }
}

The buttons array is initialize in data as an empty array. when my buttons array changes, the view is updated and display the correct number of buttons but my class active is not triggered.

Have you any idea.

1
  • Can you please share the data object?, also when or what triggers createPaginationButtons? Commented Nov 18, 2018 at 0:22

1 Answer 1

1

Push is a wrapped method so it will trigger a view update.

Do you have declared buttons on the data property?

data() {
     buttons:[]
}

Since you are creating the active property as i === 0 you don't need Vue.set.

I don't know if has something to do but instead of making buttons empty and then pushing, use an aux variable to create the array and then just do this.buttons = auxVariable. This should trigger an update.

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

10 Comments

Yes i have declared buttons in data property like your code. I don't understand what is aux variables ?
I mean to push the array into a different variable, a local variable of the function, and then just assing the value of this new variable to the buttons variable.
I try this option too, but it's the same result, it doesn't update my view :/
Mmm, have you checked if the array contains something? It is weird. Maybe the loop is not iterating and then the view doesn't update because the array is empty.
No everything is correct, i don't understand why too. My arrays contains the correct values, the view is updated and dispay the correct number of buttons but my class active is not triggered
|

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.