[英]Updating Existing Task Instead Of Adding A New Task (ToDo List App)
I'm building a ToDo app.我正在构建一个 ToDo 应用程序。 The feature I'm having issue with is when I go to "Edit" a task and upon clicking "Enter", it adds a New Task, rather than updating the existing task.
我遇到的问题是当我去“编辑”一个任务并单击“Enter”时,它会添加一个新任务,而不是更新现有任务。
Simultaneously, I want to update the Date of the task in "Edit".同时,我想在“编辑”中更新任务的日期。 I have an EventListener tied to 'Set Date' button, inside of my datepicker.
我的日期选择器内有一个与“设置日期”按钮绑定的 EventListener。 Each time the 'Set Date' is clicked, it also adds a New Task, rather than updating the date.
每次单击“设置日期”时,它还会添加一个新任务,而不是更新日期。
// edit task function
function editTask(taskId, taskName) {
editId = taskId;
isEditedTask = true;
taskInput.value = taskName;
element.classList.add("show__date")
}
// enter feature
taskInput.addEventListener("keyup", e => {
let userTask = taskInput.value.trim();
if(e.key == "Enter" && userTask ) {
// alert(When Is This Task Due? 😁")
element.classList.add("show__date");
} document.addEventListener("click", () => {
// removing show class from the task menu on the document click
if(e.target !== insertDate) {
element.classList.remove("show__date");
}
});
})
// set date feature
insertDate.addEventListener("click", () => {
if (dateSelect.value.length !== "") { // checking to see if .date__wrapper input
field is empty
element.classList.remove("show__date"); // removing datepicker, if field is
not empty after clicking button
let userTask = taskInput.value.trim();
let dueDate = dateSelect.value.trim();
let taskInfo = {name: userTask, status: "pending", date: dueDate};
todos.push(taskInfo); // adding new task to todos
}
// else if (!isEditedTask) {
// if(!todos) { //if todos doesn't exist, pass an empty array to todos
// todos = [];
// }
// }
localStorage.setItem("todo__list", JSON.stringify(todos));
showTodo(); //displays new task inside the task menu once enter is keyed
showTodo("all")
})
So I click "Edit" on "Task 2" and it fills the input field and I change the new task name to Task 12 and press "Enter".所以我在“任务 2”上单击“编辑”,它会填充输入字段,我将新任务名称更改为任务 12,然后按“Enter”。 After clicking "Enter", the datepicker shows and I choose a date.
单击“Enter”后,日期选择器显示,我选择了一个日期。 After clicking "Set Date", instead of updating the existing task, it adds a new task entirely.
单击“设置日期”后,它不会更新现有任务,而是完全添加一个新任务。 So Task 1, Task 2, Task 3, and Task 12. Instead of Task 1, Task 12, Task 3.
所以任务 1、任务 2、任务 3 和任务 12。而不是任务 1、任务 12、任务 3。
In the insertDate.addEventListener("click"...在 insertDate.addEventListener("click"...
You always take the inputs and then add a new todo.你总是接受输入,然后添加一个新的待办事项。 You should first check if you are currently editing and if you are, take the todo you are editing and change it instead of pushing a new one in the list.
您应该首先检查您当前是否正在编辑,如果是,请获取您正在编辑的待办事项并更改它,而不是在列表中推送新的待办事项。
it would look something like this:它看起来像这样:
// set date feature
insertDate.addEventListener("click", () => {
if (dateSelect.value.length !== "") { // checking to see if .date__wrapper input
field is empty
element.classList.remove("show__date"); // removing datepicker, if field is not empty after clicking button
let userTask = taskInput.value.trim();
let dueDate = dateSelect.value.trim();
if (editId) {
const todo = todos.find(todo => todo.id === editId);
todo.name = userTask;
todo.date = dueDate
} else {
let taskInfo = {name: userTask, status: "pending", date: dueDate};
todos.push(taskInfo); // adding new task to todos
}
}
// else if (!isEditedTask) {
// if(!todos) { //if todos doesn't exist, pass an empty array to todos
// todos = [];
// }
// }
localStorage.setItem("todo__list", JSON.stringify(todos));
showTodo(); //displays new task inside the task menu once enter is keyed
showTodo("all")
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.