简体   繁体   English

从 localStorage 中删除记录时刷新变量

[英]Refreshing a variable when records deleted from localStorage

I have a localStorage object like this:我有一个像这样的 localStorage 对象:

Key: jpxun关键字: jpxun

Value: [{"id":"0","name":"royal"},{"id":"1","name":"tippins"},{"id":"4","name":"leviosa"},{"id":"5","name":"vicious"}]值: [{"id":"0","name":"royal"},{"id":"1","name":"tippins"},{"id":"4","name":"leviosa"},{"id":"5","name":"vicious"}]

I have this JS to display the localStorage records:我有这个 JS 来显示 localStorage 记录:

    // get localStorage info
    
    var jpxun = JSON.parse(localStorage.getItem('jpxun')) || [];

    // get localStorage length      
    if (jpxun) {
        var jpxun_length = jpxun.length;
    } else {
        var jpxun_length = 0;
    }

    // assign variables for HTML ID elements
    var list_items = document.getElementById("usernames");
    var plaintext_textarea = document.getElementById("plaintext");

    // assign a MyUsernames variable        
    var MyUsernames = JSON.parse(localStorage.getItem("jpxun"));
    
    // if localStorage length > 0
    if (jpxun_length > 0) {
        
        // loop through localStorage
        // get the word part of the username and wrap in list item HTML
        // add a link in the list item to delete the localStorage ite via 'deleteById'
        
        for (var i = 0; i < MyUsernames.length; i++) {

            // assign a variable to hold the username
            var un1 = MyUsernames[i].name;

            list_items.innerHTML += "<li>" +"<a id="+MyUsernames[i].id + " href='#' onclick='deleteById(this)'>" + un1 + "</a></li>";
            
            // build plaintext version of username list
            plaintext_textarea.innerHTML += un1 + "\n";
        }

    }
    
    // function to delete records from localStorage

    var deleteById = function ( self ) {
        
        MyUsernames = MyUsernames.filter(function(elem) {
            return elem.id !== self.id;
        });

        localStorage.setItem("jpxun",JSON.stringify(MyUsernames));
        self.parentNode.parentNode.removeChild(self.parentNode);
                            
        // try to re-do plaintext content
        
        var jpxun2 = JSON.parse(localStorage.getItem('jpxun')) || [];
        var MyUsernames2 = JSON.parse(localStorage.getItem("jpxun2"));
    
        for (var i = 0; i < MyUsernames2.length; i++) {
            var un1 = MyUsernames[i].name;
            plaintext_textarea.innerHTML += un1 + "\n";
        }
    
    }

I realise that's a bit of code overload...我意识到这有点代码重载......

Basically it all works - the thing I can't get to work is that when I delete a record from localStorage, eg by clicking HTML for a list item for a word, which might look like this:基本上一切正常 - 我无法开始工作的事情是,当我从 localStorage 中删除一条记录时,例如通过单击 HTML 以获取一个单词的列表项,它可能如下所示:

<li><a id="1" href="#" onclick="deleteById(this)">tippins</a></li>

Then the record is deleted from localStorage, and div id usernames containing the words as list items is automatically refreshed, as the deleted record is removed.然后从 localStorage 中删除该记录,并且当删除的记录被删除时,包含单词作为列表项的 div id usernames会自动刷新。

However, the list of words in the textarea is not refreshed (even though it is built up from the list of items in localStorage).但是,textarea 中的单词列表不会刷新(即使它是根据 localStorage 中的项目列表构建的)。

Here's what I tried to refresh the textarea list of words when a word is deleted from localStorage in the deleteById function:这是我在deleteById函数中从 localStorage 中删除单词时尝试刷新单词的 textarea 列表的内容:

// try to re-do plaintext content

var jpxun2 = JSON.parse(localStorage.getItem('jpxun')) || [];
var MyUsernames2 = JSON.parse(localStorage.getItem("jpxun2"));

for (var i = 0; i < MyUsernames2.length; i++) {
    var un1 = MyUsernames2[i].name;
    plaintext_textarea.innerHTML += un1 + "\n";
}

However - that doesn't work, and the textarea list is not updated and the deleted words still appear in that.但是 - 这不起作用,并且 textarea 列表没有更新,删除的单词仍然出现在其中。

The only way the textarea list is updated is when the page reloads, but I'd like to have the list automatically update when a record is deleted from localStorage, in the same way the list items are automatically updated.更新 textarea 列表的唯一方法是在页面重新加载时,但我希望在从 localStorage 中删除记录时自动更新列表,与自动更新列表项的方式相同。

I'd simply create a function with a single responsibility to render the content of the textarea based on the data stored in the localStorage , like below我只是创建一个具有单一职责的函数来根据localStorage存储的数据呈现 textarea 的内容,如下所示

function renderTextArea() {
  let data = JSON.parse(localStorage.getItem("jpxun")) || [];
  let content = data.map(u => u.name).join("\n");
  plaintext_textarea.innerHTML = content;
}

and then just call this function in your deleteById .然后只需在您的deleteById调用此函数。

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

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