目录
vue2写todolist
template部分
<div>
<div class="top">
<div>Todolist</div>
<input type="text" v-model.trim="inp" @keyup.enter="add" />
</div>
<div class="wwc">
<h4>还未完成{{ num }}</h4>
<div
v-for="(item, index) in todo"
:key="index"
v-show="!item.checked"
class="sb"
>
<div>
<input
type="checkbox"
:checked="item.checked"
@change="addHandle(index)"
/>{{ item.title }}
</div>
<button @click="del(index)">-</button>
</div>
</div>
<div class="wwc">
<h4>已经完成{{ unm }}</h4>
<div v-for="(item, index) in todo" :key="index" v-show="item.checked">
<input
type="checkbox"
:checked="item.checked"
@change="addHandle(index)"
/><span>{{ item.title }}</span>
<button @click="del(index)">-</button>
</div>
</div>
</div>
script部分
export default {
data() {
return {
inp: "",
todo: [],
};
},
computed: {
num() {
var num = 0;
num = this.todo.filter((item) => item.checked == false).length;
return num;
},
unm() {
var num = 0;
num = this.todo.filter((item) => item.checked == true).length;
return num;
},
},
methods: {
add() {
this.todo.push({
checked: false,
title: this.inp,
});
this.inp = "";
},
addHandle(index) {
this.todo[index].checked = !this.todo[index].checked;
},
del(index) {
this.todo.splice(index, 1);
},
},
};
css部分
.top {
width: 100%;
height: 40px;
color: white;
line-height: 40px;
background-color: #000;
display: flex;
justify-content: space-around;
input {
width: 200px;
height: 27px;
margin-top: 5px;
}
}
.wwc {
width: 60%;
margin: 10px auto;
.sb {
width: 100%;
height: 30px;
display: flex;
justify-content: space-between;
}
button {
width: 30px;
height: 30px;
border-radius: 50%;
}
}