I'm trying to make a client-side to-do list using VueJS (2.x). This is my HTML:
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>To-do List</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> </head> <body> <div id="app"> <h1>To-do List</h1> <h2>Completed Tasks!</h2> <ul> <li v-for="item in complete">{{ item.description }}<input type="checkbox" :checked="item.done" @change="item.done = false"></li> </ul> <h2>Uncompleted Tasks!</h2> <ul> <li v-for="item in uncomplete">{{ item.description }}<input type="checkbox" :checked="item.done" @change="item.done = true"></li> </ul> </div> <script src="https://cdn.jsdelivr.net/npm/vue@2.7.13/dist/vue.js"></script> <script type="module" src="scripts/app.js"></script> </body> </html>
Then in scripts/app.js I did this:
'use strict' let app = new Vue({ el : "#app", data : { tasks: [ { description: 'Say Hello', done: true }, { description: 'Review Code', done: false }, { description: 'Read Emails', done: false }, { description: 'Reply to Emails', done: false }, { description: 'Wash The Dishes', done: true }, { description: 'Stop Working', done: true }, ] }, computed : { complete : function() { return this.tasks.filter(task => task.done === true); }, uncomplete : function() { return this.tasks.filter(task => task.done === false); } } });
My problem is simple: when I change the state of a checkbox in a given list (check or uncheck it), the checkbox immediately following it also switches state.
I don't understand why this happens and how to fix it. If anyone could tell me why this is happening (so that I don't have to come back here when v-for
/switch-state misbehaves) and how to fix it, that would be great!
Function
instead ofObject
indata
, otherwise it may cause update errors.Because
Function
is only accepted when used in a component definition.compulated
property, which has only oneCompulated Getter
by default. If you want to handle acompated
property, give acomputed setter
to it.v-for
because vue2 does not recognize changes in the array.