简体   繁体   English

在页面上显示本地存储数据

[英]Display Local Storage Data on Page

I know there is a similar post about displaying local storage data, but my structure wasn't quite like it and I wasn't able to figure out how could I do it.我知道有一篇关于显示本地存储数据的类似帖子,但我的结构不太像它,我无法弄清楚我该怎么做。 So here it goes.就这样吧。

PS: There might be some syntax/logical mistakes regarding the ES6 (I'm still trying to get used to it) PS:关于 ES6 可能存在一些语法/逻辑错误(我仍在尝试习惯它)

So here is my JS File:所以这是我的 JS 文件:

const list = document.getElementById("list");
const input = document.getElementById("new-todo");
let toDoList = [];
let toDoObj = {
    toDoName: String,
    toDoDone: Boolean,
    toDoID: 0,
}



input.addEventListener("keyup", e => {
    if (e.keyCode === 13) {
        addToDo(input.value, toDoObj);
        updateLocalStorage();
    }
});

list.addEventListener("click", e => {
    ulComp = e.target;
    if(ulComp.id === "dltBtn") {
        removeToDo(ulComp, toDoList);
        updateLocalStorage();
    }
    else if (ulComp.classList.contains("todo-text")) {
        doneToDo(ulComp, toDoList);
        updateLocalStorage();
    }
});


const addToDo = (newToDo, obj) => {
    if(input.value) {
        const toDoItem = `<li id="item"><i class="fas fa-trash-alt" data-text="${newToDo}" id="dltBtn"></i><span data-text="${newToDo}" class="todo-text">${newToDo}</span> </li>`;
        list.insertAdjacentHTML("afterbegin", toDoItem );
        obj = {
            toDoName: newToDo,
            toDoDone: false,
            toDoID: toDoObj.toDoID++
        };
        toDoList.push(obj);
        input.value = "";
    }
    else {
        alert("Please Type In A To-Do!");
    }
}
const doneToDo = (item, list) => {
    item.classList.toggle("done");
    for(let i = 0; i < list.length; i++) {
        if (list[i].toDoName === item.getAttribute("data-text") && item.classList.contains("done")) {
            list[i].toDoDone = true;
        }
        else if (list[i].toDoName === item.getAttribute("data-text")) {
            list[i].toDoDone = false;
        }
    }
}

const removeToDo = (item, list) => {
    for (let i = 0; i < list.length;i++) {  
        if (list[i].toDoName === item.getAttribute("data-text")) {
            item.parentElement.remove();
            list.splice(i,1);
            console.log(list);
        }
    }
}

const updateLocalStorage = () => {
    localStorage.setItem("TODOs", JSON.stringify(toDoList));
}

const loadLocalStorage = () => {
    JSON.parse(window.localStorage.getItem('TODOs'));
}

So far I was able to update the data on certain actions and getting it back on refresh.到目前为止,我能够更新某些操作的数据并在刷新时恢复。 But I couldn't figure out how to display it.但我不知道如何显示它。

I've tried passing JSON.parse(window.localStorage.getItem('TODOs'));我试过通过JSON.parse(window.localStorage.getItem('TODOs')); attributes and values into the variable and call the addToDo() function, but since it's taking input.value and toDoObj arguements, I couldn't get it working.属性和值到变量中并调用addToDo() function,但由于它采用input.valuetoDoObj争论,我无法让它工作。

Basically, (if I have no other mistakes in my logic) I want to be able to show the items on my locally stored array on the page when the page is refreshed.基本上,(如果我的逻辑没有其他错误)我希望能够在页面刷新时在页面上显示我本地存储的数组中的项目。

Any help is very much appreciated很感谢任何形式的帮助

PS 2: I'm still a newbie, so I'm sorry the code is messy and if there are some logical mistakes. PS 2:我还是一个新手,所以很抱歉代码很乱,如果有一些逻辑错误。 ( feedback on those are appreciated as well. (对这些的反馈也值得赞赏。

Thanks!谢谢!

Snippet:片段:

 const list = document.getElementById("list"); const input = document.getElementById("new-todo"); let toDoList = []; let toDoObj = { toDoName: String, toDoDone: Boolean, toDoID: 0, } input.addEventListener("keyup", e => { if (e.keyCode === 13) { addToDo(input.value, toDoObj); updateLocalStorage(); } }); list.addEventListener("click", e => { ulComp = e.target; if(ulComp.id === "dltBtn") { removeToDo(ulComp, toDoList); updateLocalStorage(); } else if (ulComp.classList.contains("todo-text")) { doneToDo(ulComp, toDoList); updateLocalStorage(); } }); const addToDo = (newToDo, obj) => { if(input.value) { const toDoItem = `<li id="item"><i class="fas fa-trash-alt" data-text="${newToDo}" id="dltBtn"></i><span data-text="${newToDo}" class="todo-text">${newToDo}</span> </li>`; list.insertAdjacentHTML("afterbegin", toDoItem ); obj = { toDoName: newToDo, toDoDone: false, toDoID: toDoObj.toDoID++ }; toDoList.push(obj); input.value = ""; } else { alert("Please Type In A To-Do;"), } } const doneToDo = (item. list) => { item.classList;toggle("done"); for(let i = 0. i < list;length. i++) { if (list[i].toDoName === item.getAttribute("data-text") && item.classList.contains("done")) { list[i];toDoDone = true. } else if (list[i].toDoName === item.getAttribute("data-text")) { list[i];toDoDone = false, } } } const removeToDo = (item; list) => { for (let i = 0. i < list;length.i++) { if (list[i].toDoName === item.getAttribute("data-text")) { item.parentElement;remove(). list,splice(i; 1). } } } const updateLocalStorage = () => { localStorage,setItem("TODOs". JSON;stringify(toDoList)). } const loadLocalStorage = () => { JSON.parse(window.localStorage;getItem('TODOs')); }
 .done { text-decoration: line-through; opacity: 0.5; }
 <,DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>To Do List</title> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="style:css"> <.-- FONT AWESOME --> <script src="https.//kit.fontawesome.com/b9a2aee93e.js" crossorigin="anonymous"></script> </head> <body> <div class="container"> <h1>To Do List</h1> <input id="new-todo" type="text" name="todo"> <ul id="list"> </ul> </div> <script type="text/javascript" src="main.js"></script> </body> </html>

I think you have to call addToDo() for all the items in the retrieved array.我认为您必须为检索到的数组中的所有项目调用addToDo() You can do something like this.你可以做这样的事情。

    const items = JSON.parse(window.localStorage.getItem('TODOs'));
    items.forEach((item) => {
        addTodo(item)
    });
const localStorageLoad = () => {
    const items = JSON.parse(window.localStorage.getItem('TODOs'));
    items.forEach( (item) => {
        addToDo(item.toDoName, item);
    });
}

Editing localStorageLoad function as above and calling the function on window load as below, made everything work perfectly.如上所述编辑localStorageLoad function 并在 window 上调用 function 加载如下,使一切正常。

window.addEventListener("load", (e) => {
    localStorageLoad();
});

So Checking Aditya's answer.所以检查阿迪亚的答案。 It was mostly the correct answer in terms of logic.就逻辑而言,这主要是正确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM