vue_todoList
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>TodoList App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./todoList.css">
<script src="./vue.min.js"></script>
</head>
<body>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<div class="todo-header">
<input
type="text"
placeholder="请输入你的任务名称,按回车键确认"
v-model="title"
@keyup.enter="addTodo"/>
</div>
<ul class="todo-main">
<transition-group appear name="todo">
<li v-for="todo in todos" :key='todo.id' @dblclick="handleUpdate(todo)" :tabIndex='todo.id'>
<label >
<input type="checkbox"
:checked="todo.done"
@change="checkTodo(todo)"
/>
<span v-show="!todo.isEdit">{{todo.title}}</span>
<input
type="text"
class="todo-edit"
v-show="todo.isEdit"
:value="todo.title"
@blur="handleBlur(todo,$event)"
ref="inputTitle"
:id="todo.id"
@keyup.enter="handleBlur(todo,$event)"
/>
</label>
<button class="btn btn-danger" @click="deleteTodo(todo.id)" >删除</button>
<button class="btn btn-edit" @click="handleUpdate(todo)" >编辑</button>
</li>
</transition-group>
</ul>
<div class="todo-footer" v-show="total">
<label>
<input type="checkbox" v-model="isAllChecked"/>
</label>
<span>
<span>已完成{{doneTotal}}</span>/ 全部{{total}}
</span>
<button class="btn btn-danger" @click="clearCompletedTodos" tabIndex='0'>清除已完成任务</button>
</div>
</div>
</div>
</div>
<script>
new Vue({
el: '#root',
data(){
return{
todos:JSON.parse(localStorage.getItem('todos')) || [],
title:''
}
},
methods:{
//添加需要完成事项
addTodo(){
if (!this.title.trim()) return alert('输入不能为空!')
//let id= this.todos.length+1
let id = 0
if(this.todos.length>0){
let arr=[]
let max = 0
this.todos.forEach((todo)=>{
if(max < todo.id){
max = todo.id
}
})
id = max + 1
}
const todoObj={id:id,title:this.title,done:false}
this.todos.unshift(todoObj)
this.title=''
},
//勾选已经完成事项
checkTodo(todo){
todo.done = !todo.done;
},
//删除已经完成事项
deleteTodo(id){
if(confirm("确定删除吗?")){
this.todos = this.todos.filter((todo)=>{
return todo.id !== id
})
}
},
//清除已经完成事项
clearCompletedTodos(){
if(confirm("确定清空所有已完成任务吗?")){
this.todos=this.todos.filter((todo)=>{
return !todo.done
})
}
},
//处理显示文本编辑框
handleUpdate(todo){
if(todo.hasOwnProperty('isEdit')){
todo.isEdit = true
}else{
this.$set(todo,'isEdit',true)
}
//等待画面渲染完毕后设定焦点
this.$nextTick(function(){
this.$refs.inputTitle.forEach((input)=>{
if(todo.id == input.id){
input.focus()
}else{
}
})
})
},
//编辑文本处理
handleBlur(todo,e){
todo.isEdit = false
if(!e.target.value.trim()) return alert('输入不能为空!')
if(todo.title != e.target.value){
todo.title = e.target.value
todo.done = false
}
}
},
//深度监视未完成事项列表
watch:{
todos:{
deep:true,
handler(value){
localStorage.setItem('todos',JSON.stringify(value))
}
}
},
computed: {
//统计总的未完成事项件数
total() {
return this.todos.length
},
//统计已经完成事项件数
doneTotal(){
return this.todos.reduce((pre,todo)=>{
console.log(pre)
return pre + (todo.done?1:0)
},0)
},
isAllChecked:{
//判断是否全选
get(){
return this.doneTotal == this.total && this.total>0
},
//设置全选和全不选
set(done){
this.todos.forEach((todo)=>{
return todo.done = done
})
}
}
}
})
</script>
</body>
</html>
todoList.css
/*base*/
body {
background: #fff;
}
.btn {
/* display: inline-block; */
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn-edit {
color: #fff;
background-color:rgb(62, 104, 244);
border: 1px solid rgb(62, 104, 244);
margin-right: 5px;
}
.btn-edit:hover {
color: #fff;
background-color: rgb(27, 78, 244);
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);
}
/*main*/
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
}
.todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
background-color: beige;
box-shadow:inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(164, 236, 82, 0.6)
}
li label {
float: left;
cursor: pointer;
}
li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
}
li button{
display: none;
float: right;
margin-top: 3px;
}
li:before {
content: initial;
}
li:last-child {
border-bottom: none;
}
li:hover{
background-color: #ddd;
}
li:hover button{
display: block;
}
.todo-edit {
width: 400px;
height: 20px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
}
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
}
.todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
}
.todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
}
.todo-footer button {
float: right;
margin-top: 5px;
}
input[type=checkbox] {
position: relative;
width: 16px;
height: 16px;
cursor: pointer;
}
input[type=checkbox]::after {
position: absolute;
top: 0;
color: #000;
width: 16px;
height: 16px;
display: inline-block;
visibility: visible;
padding-left: 0px;
text-align: center;
content: ' ';
border-radius: 3px
}
input[type=checkbox]:checked::after {
content: "✓";
color: #fff;
font-size: 12px;
line-height: 16px;
font-weight: bold;
background-color: green;
}
.todo-enter-active{
animation: todo 0.5s linear;
}
.todo-leave-active{
animation: todo 0.5s linear reverse;
}
@keyframes todo {
from{
transform: translateX(100%);
}
to{
transform: translateX(0px);
}
}