console
const app = new Vue({
el: "#app",
data: {
inp: "",
todoList: [],
checkList: []
},
methods: {
add() {
if (!this.inp) {
alert("请输入信息!")
} else {
this.todoList.push({
isdo: false,
info: this.inp
})
}
},
del(index) {
this.todoList.splice(index, 1)
},
trueCount() {
let count = [...this.todoList].filter(item => {
return item.isdo == true
}).length;
return count;
},
falseCount() {
let count = [...this.todoList].filter(item => {
return item.isdo == false
}).length;
return count;
}
},
mounted() {
if (localStorage.todoList) {
this.todoList = JSON.parse(localStorage.todoList);
}
this.todoList.forEach((item, index) => {
if (item.isdo) { this.checkList.push(index) }
})
this.$watch("checkList",function(){
this.todoList.forEach((item,index)=>{
let i = this.checkList.indexOf(index)
if(i!=-1){
item.isdo=true;
console.log(item.info);
}else{
item.isdo=false;
}
})
localStorage.setItem("todoList", JSON.stringify(this.todoList))
})
},
watch: {
todoList() {
localStorage.setItem("todoList", JSON.stringify(this.todoList))
},
}
})
<div id="app">
<div class="head">
<div class="content">
<h2>TodoList</h2>
<div class="right">
<input type="text" v-model="inp" @keyup.enter="add">
<button @click="add">添加</button>
</div>
</div>
</div>
<div class="item_box">
<div class="item">
<div class="title">
<h2>正在进行</h2>
<span>{{falseCount()}}</span>
</div>
<ul>
<li v-for="(item,index) in todoList" :key="index" v-if="!item.isdo">
<input type="checkbox" :key="index" :value="index" v-model="checkList">
{{item.info}}
<button :id="index" @click="del(index)">删除</button>
</li>
</ul>
</div>
<div class="item">
<div class="title">
<h2>已经完成</h2>
<span>{{trueCount()}}</span>
</div>
<ul>
<li v-for="(item,index) in todoList" :key="index" v-if="item.isdo">
<input type="checkbox" :key="index" :value="index" v-model="checkList">
{{item.info}}
<button :id="index" @click="del(index)">删除</button>
</li>
</ul>
</div>
</div>
</div>
* {
margin: 0;
padding: 0;
list-style: none;
}
html,
body {
height: 100%;
}
#app {
height: 100%;
background-color: #ff6b6b;
display: flex;
flex-direction: column;
align-items: center;
}
.head {
padding: 20px 0;
background-color: #ff9f43;
width: 100%;
}
.content {
margin: 0 auto;
width: 50%;
display: flex;
justify-content: space-between;
padding-right: 20px;
box-sizing: border-box;
}
.content input {
border-radius: 50px;
font-size: 18px;
outline: none;
padding-left: 1%;
}
.content .right {
display: flex;
align-items: center;
}
.content .right button {
flex-shrink: 0;
}
.item_box {
width: 100%;
}
.item_box .item {
margin: 0 auto;
width: 50%;
}
.item_box .item .title {
display: flex;
align-items: center;
justify-content: space-between;
}
.item_box .item .title span {
display: inline-block;
width: 25px;
height: 25px;
text-align: center;
background-color: #feca57;
border-radius: 50px;
line-height: 25px;
}
.item_box .item ul li {
border-left: 5px #01a3a4 solid;
box-sizing: border-box;
padding: 5px;
background-color: #c8d6e5;
border-radius: 5px;
margin: 10px 0;
}