console
var ls = window.localStorage;
var lsLists = new Array();
if (ls.getItem("lists") != null) {
lsLists = JSON.parse(ls.getItem("lists"));
}
var vm = new Vue({
el: '#app',
data: {
lists:lsLists,
todo:{
content:'',
finished:false,
deleted:false
}
},
methods:{
addTodo(){
this.lists.unshift(this.todo);
this.lists.sort(sortF);
ls.setItem("lists",JSON.stringify(this.lists));
this.todo = {
content : '',
finished:false,
deleted:false
};
},
changeStatus(i){
let thisStatus = this.lists[i].finished;
this.lists[i].finished = !thisStatus;
this.lists.sort(sortF);
ls.setItem("lists",JSON.stringify(this.lists));
},
delItem(i){
this.lists.splice(i,1);
ls.setItem("lists",JSON.stringify(this.lists));
}
}
});
function sortF(a,b){
console.log(a.finished,b.finished)
return a.finished-b.finished;
}
<div id="app">
<div>
<input type="text" class="addInput" placeholder="请输入待办事项" v-model="todo.content" @keydown.enter="addTodo">
</div>
<div class="unit" v-for="(item,index) in lists">
<input type="checkbox" class="cb" :checked="item.finished" @click="changeStatus(index)">
<span :class="{'finish':item.finished,'no':true}">{{index+1}}</span>
<span :class="{'finish':item.finished,'con':true}">{{item.content}}</span>
<button type="button" class="delBtn" @click="delItem(index)">×</button>
</div>
<div class="empty" v-if="lists.length === 0">
暂无内容!
</div>
</div>
html{
width: 350px;
}
.addInput{
display: block;
width: 330px;
height: 25px;
border: 1px #ddd solid;
border-radius: 5px;
padding: 2px;
margin: 20px 0;
box-sizing: border-box;
box-shadow: 0 0 5px #ddd;
}
.unit{
position:relative;
border-bottom: 1px #ddd solid;
padding-bottom: 3px;
margin: 3px 0;
}
.finish{
text-decoration: line-through;
color:#9e9e9e;
}
.delBtn{
position: absolute;
right: 0;
border: 0;
color: #fbf0f0;
background-color: #F44336;
border-radius: 5px;
width:15%;
}
.cb,.no{
width:10%;
background: #ddd;
border: solid #ddd;
border-width: 1px 5px 1px 5px;
border-radius: 50%;
}
.con{
word-wrap:break-word;
word-break: break-all;
width: 65%;
}
.empty{
font-size: 15px;
color: #ccc;
text-align: center;
padding: 10px 0;
}