Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
React to object property change in an Array in Vue 3

I have a Vue 3 app. In this app, I need to show a list of items and allow a user to choose items in the array. Currently, my component looks like this:

MyComponent.vue

<template>
  <div>
    <div v-for="(item, index) in getItems()" :key="`item-${itemIndex}`">
      <div class="form-check">
        <input class="form-check-input" :id="`checkbox-${itemIndex}`" v-model="item.selected" />
        <label class="form-check-label" :for="`checkbox-${itemIndex}`">{{ item.name }} (selected: {{ item.selected }})</label>
      </div>
    </div>

    <button class="btn" @click="generateItems">Generate Items</button>
  </div>
</template>

<script>
  import { reactive } from 'vue';

  export default {
    data() {
      return itemCount: 0
    },

    methods: {
      generateItems() {
        this.itemCount = Math.floor(Math.random() * 25) + 1;
      },

      getItems() {
        let items = reactive([]);
        for (let i=0; i<this.itemCount; i++) {
          items.push({
            id: (i+1),
            selected: false,
            name: `Item #${i+1}`
          });
        }
        return items; 
      }
    }
  }
</script>

When I click select/deselect the checkbox, the "selected" text does not get updated. This tells me that I have not bound to the property properly. However, I'm also unsure what I'm doing wrong.

How do I bind a checkbox to the property of an object in an Array in Vue 3?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you set a breakpoint in getItems(), or output a console.log in there, you will notice every time that you change a checkbox selection, it is getting called. This is because the v-for loop is re-rendered, and it'll call getItems() which will return it a fresh list of items with selected reset to false on everything. The one that was there before is no longer in use by anything.

To fix this, you could only call getItems() from generateItems() for example, and store that array some where - like in data, and change the v-for to iterate that rather than calling the getItems() method.

about 4 years ago · Juan Pablo Isaza Report

0

Steve is right.

Here is a fixed version: Vue SFC Playground.

<template>
  <div>
    <div v-for="(item, index) in items" :key="`item-${index}`">
      <div class="form-check">
        <input type="checkbox" class="form-check-input" :id="`checkbox-${index}`" v-model="item.selected" />
        <label class="form-check-label" :for="`checkbox-${index}`">{{ item.name }} (selected: {{ item.selected }})</label>
      </div>
    </div>

    <button class="btn" @click="generateItems">Generate Items</button>
  </div>
</template>

<script>
  import { reactive } from 'vue';

  export default {
    data() {
      return { items: [] }
    },
    methods: {
      generateItems() {
        this.itemCount = Math.floor(Math.random() * 25) + 1;
        this.getRndItems();
      },
      getRndItems() {
        this.items = reactive([]);
        for (let i=0; i<this.itemCount; i++) {
          this.items.push({
            id: (i+1),
            selected: false,
            name: `Item #${i+1}`
          });
        }
      }      
    }
  }
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!