.container{     max-width: 700px;     margin-inline: auto;     display: flex;     align-items: center;     justify-content: center;     flex-direction: column;}form #inputTodo{     padding: 8px 15px;}form #btnSubmit{     padding: 8px 15px;}.todos-container{     padding: 10px;     width: 100%;     width: fit-content;}.todo-item{     padding: 5px;     border: 1px solid gray;     gap: 20px;     width: 100%;     display: flex;     align-items: center;}.textCut{     text-decoration: line-through;}
interface Todo{     content: string,     isComplete: boolean,     readonly id:string}let todos:Todo[] = [];const todosContainer = document.querySelector(\\'.todos-container\\')!;function genrateTodo(id: string, content:string, isComplete:boolean){     const itemContainer:HTMLDivElement = document.createElement(\\'div\\');     itemContainer.classList.add(\\'todo-item\\');     // checkbox     const checkbox: HTMLInputElement = document.createElement(\\'input\\');     checkbox.type = \\\"checkbox\\\";     checkbox.checked = isComplete     checkbox.onchange = () => {          todos.find(todo => {               if(todo.id === id) todo.isComplete = checkbox.checked;               // save updated todo               localStorage.setItem(\\'todos\\', JSON.stringify(todos));          })          todoText.className = checkbox.checked ? \\'textCut\\' : \\'\\';     }     // paragraph content     const todoText:HTMLParagraphElement = document.createElement(\\'p\\');     todoText.innerHTML = content;     todoText.className = isComplete ? \\'textCut\\' : \\'\\';     // delete button     const btnDelete: HTMLButtonElement = document.createElement(\\'button\\');     btnDelete.textContent = \\'X\\';     btnDelete.onclick = () => {          todos = todos.filter(todo => todo.id !== id);          localStorage.setItem(\\'todos\\', JSON.stringify(todos));          renderTodos(todos);     }     itemContainer.append(checkbox, todoText, btnDelete);     todosContainer.appendChild(itemContainer);}// add todoconst form = document.getElementById(\\'myForm\\')!;form.onsubmit = (e: SubmitEvent) => {     e.preventDefault();     const inputElement = document.getElementById(\\'inputTodo\\') as HTMLInputElement;     let value:string = inputElement.value;     const newTodo:Todo = {          id: String(Math.random() * 1000),          content: value,          isComplete: false     }     todos.push(newTodo);     inputElement.value = \\\"\\\";     localStorage.setItem(\\'todos\\', JSON.stringify(todos));     renderTodos(todos);}function renderTodos(todos: Todo[]){     todosContainer.innerHTML = \\\"\\\";     todos.forEach(todo => {          genrateTodo(todo.id, todo.content, todo.isComplete);     })}const jsonTodos = localStorage.getItem(\\'todos\\')!;const storedTodos : Todo[] = JSON.parse(jsonTodos);todos = storedTodos;renderTodos(todos);
","image":"http://www.luping.net/uploads/20240729/172225188366a77a6bd96e6.jpg","datePublished":"2024-07-29T19:18:03+08:00","dateModified":"2024-07-29T19:18:03+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > I create a Todo Application in Typescript With Save Todos in Local Storage

I create a Todo Application in Typescript With Save Todos in Local Storage

Published on 2024-07-29
Browse:194

I create a Todo Application in Typescript With Save Todos in Local Storage

we are use a vanilla vite project with typescript in this for understanding typescript and use a localstorage for store the todos.




     Vite   TS

My Todo List

.container{
     max-width: 700px;
     margin-inline: auto;
     display: flex;
     align-items: center;
     justify-content: center;
     flex-direction: column;
}

form #inputTodo{
     padding: 8px 15px;
}

form #btnSubmit{
     padding: 8px 15px;
}

.todos-container{
     padding: 10px;
     width: 100%;
     width: fit-content;
}

.todo-item{
     padding: 5px;
     border: 1px solid gray;
     gap: 20px;
     width: 100%;
     display: flex;
     align-items: center;
}

.textCut{
     text-decoration: line-through;
}
interface Todo{
     content: string,
     isComplete: boolean,
     readonly id:string
}

let todos:Todo[] = [];

const todosContainer = document.querySelector('.todos-container')!;

function genrateTodo(id: string, content:string, isComplete:boolean){
     const itemContainer:HTMLDivElement = document.createElement('div');
     itemContainer.classList.add('todo-item');

     // checkbox
     const checkbox: HTMLInputElement = document.createElement('input');
     checkbox.type = "checkbox";
     checkbox.checked = isComplete
     checkbox.onchange = () => {
          todos.find(todo => {
               if(todo.id === id) todo.isComplete = checkbox.checked;

               // save updated todo
               localStorage.setItem('todos', JSON.stringify(todos));
          })
          todoText.className = checkbox.checked ? 'textCut' : '';
     }

     // paragraph content
     const todoText:HTMLParagraphElement = document.createElement('p');
     todoText.innerHTML = content;
     todoText.className = isComplete ? 'textCut' : '';

     // delete button
     const btnDelete: HTMLButtonElement = document.createElement('button');
     btnDelete.textContent = 'X';
     btnDelete.onclick = () => {
          todos = todos.filter(todo => todo.id !== id);
          localStorage.setItem('todos', JSON.stringify(todos));
          renderTodos(todos);
     }

     itemContainer.append(checkbox, todoText, btnDelete);
     todosContainer.appendChild(itemContainer);

}

// add todo
const form = document.getElementById('myForm')!;
form.onsubmit = (e: SubmitEvent) => {
     e.preventDefault();
     const inputElement = document.getElementById('inputTodo') as HTMLInputElement;
     let value:string = inputElement.value;

     const newTodo:Todo = {
          id: String(Math.random() * 1000),
          content: value,
          isComplete: false
     }

     todos.push(newTodo);
     inputElement.value = "";

     localStorage.setItem('todos', JSON.stringify(todos));

     renderTodos(todos);
}


function renderTodos(todos: Todo[]){
     todosContainer.innerHTML = "";
     todos.forEach(todo => {
          genrateTodo(todo.id, todo.content, todo.isComplete);
     })
}

const jsonTodos = localStorage.getItem('todos')!;
const storedTodos : Todo[] = JSON.parse(jsonTodos);
todos = storedTodos;
renderTodos(todos);
Release Statement This article is reproduced at: https://dev.to/akram6t/i-create-a-todo-application-in-typescript-with-save-todos-in-local-storage-2ej3?1 If there is any infringement, please contact study_golang @163.comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3