mirror of
https://github.com/Dichgrem/Vue.git
synced 2025-12-17 14:11:58 -05:00
feat:edit_todos
This commit is contained in:
@@ -51,6 +51,11 @@ export default {
|
|||||||
this.todos = this.todos.filter((t) => t.id !== item.id);
|
this.todos = this.todos.filter((t) => t.id !== item.id);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
editTodo({ item, text }) {
|
||||||
|
item.txt = text;
|
||||||
|
console.log("编辑 Todo:", item);
|
||||||
|
},
|
||||||
|
|
||||||
toggleTodo(item) {
|
toggleTodo(item) {
|
||||||
console.log("完成状态切换:", item);
|
console.log("完成状态切换:", item);
|
||||||
},
|
},
|
||||||
@@ -75,7 +80,7 @@ export default {
|
|||||||
|
|
||||||
<template>
|
<template>
|
||||||
<TodoHeader @addTodo="addTodo" />
|
<TodoHeader @addTodo="addTodo" />
|
||||||
<TodoList :todos="todoList" @delTodo="delTodo" @move="moveTodo" />
|
<TodoList :todos="todoList" @delTodo="delTodo" @move="moveTodo" @edit="editTodo"/>
|
||||||
<TodoFooter
|
<TodoFooter
|
||||||
:count="todoList.length"
|
:count="todoList.length"
|
||||||
:tabType="tabType"
|
:tabType="tabType"
|
||||||
|
|||||||
@@ -13,9 +13,27 @@
|
|||||||
<div class="tdItem-main">
|
<div class="tdItem-main">
|
||||||
<input type="checkbox" v-model="item.completed" class="toToggle" />
|
<input type="checkbox" v-model="item.completed" class="toToggle" />
|
||||||
|
|
||||||
<span class="tdTxt" :class="{ completed: item.completed }">
|
<!-- <span class="tdTxt" :class="{ completed: item.completed }"> -->
|
||||||
|
<!-- {{ item.txt }} -->
|
||||||
|
<!-- </span> -->
|
||||||
|
<span
|
||||||
|
v-if="editingId !== item.id"
|
||||||
|
class="tdTxt"
|
||||||
|
:class="{ completed: item.completed }"
|
||||||
|
@dblclick="startEdit(item)"
|
||||||
|
>
|
||||||
{{ item.txt }}
|
{{ item.txt }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
<input
|
||||||
|
v-else
|
||||||
|
class="editInput"
|
||||||
|
v-model="editText"
|
||||||
|
@keyup.enter="confirmEdit(item)"
|
||||||
|
@blur="confirmEdit(item)"
|
||||||
|
@keyup.esc="cancelEdit"
|
||||||
|
autofocus
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="tdItem-acts">
|
<div class="tdItem-acts">
|
||||||
@@ -99,12 +117,24 @@
|
|||||||
text-decoration: line-through;
|
text-decoration: line-through;
|
||||||
color: gray;
|
color: gray;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.editInput {
|
||||||
|
font-size: 16px;
|
||||||
|
padding: 4px;
|
||||||
|
width: 100%;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
props: ["todos"],
|
props: ["todos"],
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
editingId: null,
|
||||||
|
editText: "",
|
||||||
|
};
|
||||||
|
},
|
||||||
methods: {
|
methods: {
|
||||||
delTodo(item) {
|
delTodo(item) {
|
||||||
this.$emit("delTodo", item);
|
this.$emit("delTodo", item);
|
||||||
@@ -118,6 +148,22 @@ export default {
|
|||||||
this.$emit("move", { oldIndex: this.dragIndex, newIndex: index });
|
this.$emit("move", { oldIndex: this.dragIndex, newIndex: index });
|
||||||
this.dragIndex = null;
|
this.dragIndex = null;
|
||||||
},
|
},
|
||||||
|
startEdit(item) {
|
||||||
|
this.editingId = item.id;
|
||||||
|
this.editText = item.txt;
|
||||||
|
},
|
||||||
|
|
||||||
|
confirmEdit(item) {
|
||||||
|
const text = this.editText.trim();
|
||||||
|
if (text) {
|
||||||
|
this.$emit("edit", { item, text });
|
||||||
|
}
|
||||||
|
this.editingId = null;
|
||||||
|
},
|
||||||
|
|
||||||
|
cancelEdit() {
|
||||||
|
this.editingId = null;
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user