完成todoList的学些

new_branch1
roger 2 years ago
parent 435cefc3df
commit 7cdb221faf
  1. 22
      20_脚手架/vue_code/src/App.vue
  2. 84
      20_脚手架/vue_code/src/components/TodoBottom.vue
  3. 3
      20_脚手架/vue_code/src/components/TodoHeader.vue
  4. 19
      20_脚手架/vue_code/src/components/TodoItem.vue
  5. 25
      20_脚手架/vue_code/src/components/TodoList.vue

@ -3,8 +3,8 @@
<div class="todo-container"> <div class="todo-container">
<div class="todo-wrap"> <div class="todo-wrap">
<TodoHeader :addTodo="addTodo"/> <TodoHeader :addTodo="addTodo"/>
<TodoList :todos="todos"/> <TodoList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
<TodoBottom/> <TodoBottom :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/>
</div> </div>
</div> </div>
</div> </div>
@ -25,6 +25,7 @@ export default {
}, },
data() { data() {
return { return {
//
todos: [ todos: [
{id: "001", title: "产品", done: true}, {id: "001", title: "产品", done: true},
{id: "002", title: "开发", done: true}, {id: "002", title: "开发", done: true},
@ -32,19 +33,36 @@ export default {
] ]
} }
}, },
// 访App
// App
methods: { methods: {
// todoTodoHeader
addTodo(todoObj) { addTodo(todoObj) {
this.todos.unshift(todoObj) this.todos.unshift(todoObj)
}, },
// todo穿TodoListTodoItem
checkTodo(id) { checkTodo(id) {
this.todos.forEach((todo) => { this.todos.forEach((todo) => {
if (todo.id === id) todo.done = !todo.done if (todo.id === id) todo.done = !todo.done
}) })
}, },
// todo穿TodoListTodoItem
deleteTodo(id) { deleteTodo(id) {
this.todos = this.todos.filter(todo => todo.id !== id)
},
// todoTodoBottom
checkAllTodo(done) {
this.todos.forEach((todo) => todo.done = done)
},
// todoTodoBottom
clearAllTodo() {
this.todos = this.todos.filter((todo) => {
return !todo.done
})
} }
} }
} }

@ -1,42 +1,74 @@
<template> <template>
<div class="todo-bottom"> <div class="todo-bottom" v-show="total">
<label> <label>
<input type="checkbox"> <input type="checkbox" v-model="isAll">
</label> </label>
<span> <span>
<span>已完成0</span>/全部2 <span>已完成{{ totalDone }}</span>/全部{{ total }}
</span> </span>
<button class="btn btn-danger">清除已完成任务</button> <button class="btn btn-danger" @click="clearAll">清除已完成任务</button>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: "TodoBottom" name: "TodoBottom",
props: ['todos', 'checkAllTodo', 'clearAllTodo'],
computed: {
// todov-show
total() {
return this.todos.length
},
// todo
totalDone() {
return this.todos.reduce((pre, todo) => pre + (todo.done ? 1 : 0), 0)
},
//
// get
// setAppcheckAllTodo
isAll: {
get() {
return this.totalDone === this.total && this.total > 0
},
set(value) {
this.checkAllTodo(value)
}
}
},
methods: {
// AppclearAllTodo
clearAll() {
if (confirm("确认清除全部已完成任务?")) {
this.clearAllTodo()
}
}
}
} }
</script> </script>
<style scoped> <style scoped>
.todo-bottom{ .todo-bottom {
height: 40px; height: 40px;
line-height: 40px; line-height: 40px;
padding-left: 6px; padding-left: 6px;
margin-top: 5px; margin-top: 5px;
} }
.todo-bottom label{ .todo-bottom label {
display: inline-block; display: inline-block;
margin-right: 20px; margin-right: 20px;
cursor: pointer; cursor: pointer;
} }
.todo-bottom label input {
position: relative; .todo-bottom label input {
top: -1px; position: relative;
vertical-align: middle; top: -1px;
margin-right: 5px; vertical-align: middle;
} margin-right: 5px;
.todo-bottom button { }
float: right;
margin-top: 5px; .todo-bottom button {
} float: right;
margin-top: 5px;
}
</style> </style>

@ -11,11 +11,14 @@ export default {
name: "TodoHeader", name: "TodoHeader",
props: ['addTodo'], props: ['addTodo'],
data() { data() {
// titleplaceholder
return { return {
title: '' title: ''
} }
}, },
methods: { methods: {
// todotrim
// Apptodo
add() { add() {
if (!this.title.trim()) return alert("输入不能为空") if (!this.title.trim()) return alert("输入不能为空")
const todoObj = {id: nanoid(), title: this.title, done: false} const todoObj = {id: nanoid(), title: this.title, done: false}

@ -2,19 +2,27 @@
<li> <li>
<label> <label>
<input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"> <input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)">
<span>{{todo.title}}</span> <span>{{ todo.title }}</span>
</label> </label>
<button class="btn btn-danger">删除</button> <button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button>
</li> </li>
</template> </template>
<script> <script>
export default { export default {
name: "TodoItem", name: "TodoItem",
props: ['todo', 'checkTodo'], // todoListtodoscheckTododeleteTodo
props: ['todo', 'checkTodo', 'deleteTodo'],
methods: { methods: {
// checkTodoTodoItem
handleCheck(id) { handleCheck(id) {
this.checkTodo(id) this.checkTodo(id)
},
// deleteTodoTodoItem
handleDelete(id) {
if (confirm("确认删除?")) {
this.deleteTodo(id)
}
} }
} }
@ -55,11 +63,12 @@ li:before {
li:last-child { li:last-child {
border-bottom: none; border-bottom: none;
} }
li:hover{
li:hover {
background-color: #ddd; background-color: #ddd;
} }
li:hover button{ li:hover button {
display: block; display: block;
} }
</style> </style>

@ -1,22 +1,24 @@
<template> <template>
<div> <ul class="todo-list">
<ul class="todo-list"> <TodoItem
<TodoItem v-for="todoObj in todos"
v-for="todoObj in todos" :key="todoObj.id"
:key="todoObj.id" :todo="todoObj"
:todo="todoObj" :checkTodo="checkTodo"
:checkTodo="checkTodo" :deleteTodo="deleteTodo"
/> />
</ul> </ul>
</div>
</template> </template>
<script> <script>
import TodoItem from "@/components/TodoItem.vue"; import TodoItem from "@/components/TodoItem.vue";
export default { export default {
name: "TodoList", name: "TodoList",
// TodoItem
components: {TodoItem}, components: {TodoItem},
props: ['todos', 'checkTodo'] // ApptodoscheckTododeleteTodotodoItembypass
props: ['todos', 'checkTodo', 'deleteTodo']
} }
</script> </script>
@ -27,6 +29,7 @@ export default {
border-radius: 2px; border-radius: 2px;
padding: 0px; padding: 0px;
} }
.todo-empty { .todo-empty {
height: 40px; height: 40px;
line-height: 40px; line-height: 40px;

Loading…
Cancel
Save