일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- Raw value and Associated value
- 스위프트 클로저
- Java
- Associated Value
- CoreData Concurrency
- NSManagedObject SubClass
- 2022 부스트캠프
- 1009번
- 다익스트라 이해
- codability
- iOS Static Library 사용하는방법
- LightWeight Migration
- Swift 고차함수
- Persistent store Coordinator
- 일급 객체
- leetcode #01
- Swift LinkedList
- NSSortDescriptor
- 트레일링 클로저
- NSPredicates
- expensive operation
- dateFormatter
- persistentStoreCoordinator
- Clean swift
- CoreData
- Swift closure
- CoreData Stack
- CoreData Filter
- Swift
- iOS Static Library
- Today
- Total
하루를살자
JS Clone Project [to-do list] - 3, Saving to-do list 본문
Key Functions
1. LocalStorage can only save the text.
2. pushing item into Array
3. I have tried to set the Storage value in an array form but in text --> used JSON.Stringify
4. Use of JSON.parse --> makes string into JSON format
5. Use of forEach on each item of an array
6. It is now able to show the toDo list after refreshing the page.
6. Deletion on the page --> Not able to delete from the localStorage yet.
const todoForm = document.querySelector("#todo-form");
const todoList = document.querySelector("#todo-list");
const todoInput = document.querySelector("#todo-form input");
let toDos = [];
const TO_DOS = "toDos";
//4. saving toDo list
function saveToDos(event) {
//save predefined toDos array.
localStorage.setItem(TO_DOS, JSON.stringify(toDos));
}
//3. Delete selected todo list
function deleteTodo(event){
//We can identify which li called this event by inspecting event.target object.
console.log(event.target);
console.dir(event.target.parentElement.innerText);
//I designate the selected li element
const li = event.target.parentElement;
//Remove the list
li.remove();
}
//2. Creating HTML element
function paintToDo(newTodo){
//Desired structure of HTML is
// <li>
// <span> lalalal </span>
// <button> X </button>
//</li>
const list = document.createElement("li");
list.id = newTodo.id
const span = document.createElement("span");
span.innerHTML = newTodo.text;
const button = document.createElement("button");
button.innerHTML = "X";
//Calls for deleteing selected toDo list function
button.addEventListener("click", deleteTodo);
list.appendChild(span);
list.appendChild(button);
todoList.appendChild(list);
}
//1. Make event handler of TO-DO list form
function handleToDoSubmit(event){
event.preventDefault();
const newTodo = todoInput.value;
//empty the input of the form
todoInput.value = "";
//Create toDo Object
const newTodoObj = {
text:newTodo,
id :Date.now(),
}
//Push new item into to-do list
toDos.push(newTodoObj);
//Calling a paint function
paintToDo(newTodoObj);
//Calling save toDos function
saveToDos();
}
todoForm.addEventListener("submit",handleToDoSubmit);
// function sayHi(item){
// console.log("Hello!" + item);
// }
//Retrive saved local storage toDos array
const savedtoDos = localStorage.getItem(TO_DOS);
//check whether savedtoDos are empty or not
if(savedtoDos !== null) {
const parsedToDos = JSON.parse(savedtoDos);
//Let the list to stay in the web after refreshing the page. This however still makes problem, writing over the existing
//savedtoDos when refreshed.
toDos = parsedToDos;
parsedToDos.forEach(paintToDo);
}
Improvements to be taken.
- Deleting toDos on only at the page when pressing 'X'
--> To properly delete elements of the toDos array, consideration of duplicate deletion of elements has to be taken.
Ex) If same elements are within the array of toDos[], as ["A","B","A"], when deleting "A", it will delete 2 elements in the array. Therefore, giving an "unique" ID on each element would be one possible way to resolve this problem.
--> How do we give "unique" ID?
The way that it was suggested was to implement Date.now() which outputs current time in milliseconds. We can create toDo object as the user submits the toDo form, with ID, and text value and push it into predefined toDos [].
*Note that the toDos has to be in "let" type, so that when the page is refreshed, it has whatever it was inside of localStorage.
-------------------------------------------------------------------------------------------------------------------------------
New staff learned
@paintTodo
- The const "list" which was used to define the document element of <li>, can be used to make its "id" by simply typing "list.id".
@parsedToDos.foreach()
- I have assigned paintTodo as parameter of foreach(), and this would allow calling patinTodo function for each element it has of parsedToDos. The argument of paintTodo() would have each object it was assigned in the parsedToDos.
'JS' 카테고리의 다른 글
JS Clone Project [Weather] - 1, getting location (0) | 2021.12.09 |
---|---|
JS Clone Project [to-do list] - 4, Deleting to-Do list (0) | 2021.12.09 |
JS Clone Project [to-do list] - 1,2 (0) | 2021.12.05 |
JS Clone Project [Background] (0) | 2021.12.05 |
JS Clone Project [Quotes] (0) | 2021.12.05 |