<ul>
<li v-for='todo in filteredTodos' :key='todo.id'>
<span>筛选:
<input type="button" class="selected" value="全部"
@click="intention='all'">
<input type="button" value="进行中"
@click="intention='ongoing'">
<input type="button" value="已完成"
@click="intention='completed'">
data: function () {
...
intention: 'all'
...
computed: {
...
leftTodos(){
return this.todos.filter(todo => !todo.completed)
},
leftTodoCount(){
return this.leftTodos.length
},
filteredTodos(){
if(this.intention == 'ongoing'){
return this.leftTodos
}else if (this.intention == 'completed'){
return this.todos.filter(todo => todo.completed)
}else {
//返回所有的
return this.todos
}
}
<span>筛选:
<input type="button" class="selected" value="全部"
@click="intention='all'"
v-bind:class="{selected: intention==='all'}">
<input type="button" value="进行中"
@click="intention='ongoing'"
v-bind:class="{selected: intention==='ongoing'}">
<input type="button" value="已完成"
@click="intention='completed'"
v-bind:class="{selected: intention==='completed'}">
<input type="button" value="清除已完成"
@click="clearCompleted">
<input type="button" value="清除全部"
@click="clearAll">
clearCompleted(){
this.todos = this.todos.filter(todo => !todo.completed)
},
clearAll(){
this.todos = []
}
<span>筛选:
...
<input v-if="leftTodoCount" type="button" value="进行中"
@click="intention='ongoing'"
v-bind:class="{selected: intention==='ongoing'}">
<input v-if="completedTodoCount" type="button" value="已完成"
@click="intention='completed'"
v-bind:class="{selected: intention==='completed'}">
<input v-if="completedTodoCount" type="button" value="清除已完成"
@click="clearCompleted">
computed: {
...
completedTodos(){
return this.todos.filter(todo => todo.completed)
},
completedTodoCount(){
return this.completedTodos.length
},
<div>
<span v-if="leftTodoCount">剩余 {
{ leftTodoCount }} 项未完成 ---span>
<span v-else-if="completedTodoCount">全部完成,你真是太优秀了span>
<span v-else>添加我的第一个TODOspan>
clearCompleted(){
if(!confirm('确定清除已完成的待办事项?')){
return
}
this.todos = this.todos.filter(todo => !todo.completed)
},
clearAll(){
if(!confirm('确定清除全部的待办事项?')){
return
}
this.todos = []
}
data: function () {
return {
...
recycleBin:[], //回收站
addTodo: function () {
...
this.todos.push({
id: id++, title: this.newTodoTitle,
completed:false, removed:false});
...
},
removeTodo(todo){
let pos = this.todos.indexOf(todo);
let removedTodo = this.todos.splice(pos, 1)[0];
removedTodo.removed = true;
this.recycleBin.unshift(removedTodo)
},
clearCompleted(){
if(!confirm('确定清除已完成的待办事项?')){
return
}
this.completedTodos.map(todo => todo.removed = true)
this.recycleBin.unshift(...this.completedTodos)
this.todos = this.leftTodos
},
clearAll(){
if(!confirm('确定清除全部的待办事项?')){
return
}
this.todos.map(todo => todo.removed = true)
this.recycleBin.unshift(...this.todos)
this.todos = []
}
<input v-if="recycleBin.length" type="button" value="回收站"
@click="intention='removed'">
...
filteredTodos(){
...
}else if (this.intention == 'removed'){
return this.recycleBin
...
}
从回收站还原
<li v-for='todo in filteredTodos' :key='todo.id'>
...
<input v-if="todo.removed" type="button" value="还原"
@click="restoreTodo(todo)"/>
<input v-else="todo.removed" type="button" value="删除"
@click="removeTodo(todo)"/>
restoreTodo: function(todo){
todo.removed = false
this.todos.unshift(todo) //恢复
pos = this.recycleBin.indexOf(todo)
this.recycleBin.splice(pos, 1)
},
现在只要一刷新浏览器,所有todo 都没了,因为的数据保存在内存中,页面刷新数据就会清除
可以使用浏览器的 LocalStorage 来实现数据的持续性存储
首先来定义一个对象,用于 LocalStorage 存储和获取 todo 的相关操作
为 todoStorage 对象绑定一个 uid 属性,作用是后续添加 todo 时,用于确定新添加todo 的 id
每当用户打开页面时,因为去 LocalStorage fetch 一下存储的数据
当添加 todo 时,由于可能已经存在从本地取出的 todo 数据,新的 todo id 不能是从 0 开始了,而应该从已有 todoStorage.uid 开始
定义localstorage存储和获取todo
<script>
let id = 0; // 用于 id 生成
var STORAGE_KEY = "dylan-vue2-todo"
var todoStorage = {
fetch(){
//读
var todos = JSON.parse(localStorage.getItem(STORAGE_KEY) || '[]')
todos.forEach(function(todo, index){
todo.id = index
});
todoStorage.uid = todos.length
return todos
},
save(todos){
//写
localStorage.setItem(STORAGE_KEY,JSON.stringify(todos))
}
};
data: function () {
return {
todos: todoStorage.fetch(),
...
addTodo: function () {
...
this.todos.push({
id: todoStorage.uid++, title: this.newTodoTitle,
...
watch:{
todos:{
handler: function(todos){
todoStorage.save(todos)
},
deep: true
}
},