您可以将多个单独的列表保存到localStorage吗?

时间:2019-12-05 20:25:23

标签: javascript jquery html arrays json

我正在开发一个项目,它将接受用户输入并将其存储到localStorage的列表中。我希望能够在页面上包含其中三个列表。

我已将代码放入三个单独的列中,但是当我尝试在其中提交信息时,似乎正在将所有列中的值添加到一个列表中。我并不是每个人都使用JS或Jquery,所以我一直在尝试学习,但很沮丧。这是我希望在三个单独的列中运行的代码。

如何确保列表存储在三个不同的位置,并且还分别存储在localStorage中?

       var list = JSON.parse(localStorage.getItem("todolist"));
// Checks for to dos in LS, if none starts with empty array
if (!Array.isArray(list)) {
  list = [];
}
function putOnPage() {
  $("#todo-list").empty(); // empties out the html
  var insideList = JSON.parse(localStorage.getItem("todolist"));
  if (!Array.isArray(insideList)) {
    insideList = [];
  }
  // todos to page
  for (var i = 0; i < insideList.length; i++) {
    var p = $("<p>").text(insideList[i]);
    var b = $("<button class='delete'>").text("x").attr("data-index", i);
    p.prepend(b);
    $("#todo-list").prepend(p);
  }
}
// rtodos on page load
putOnPage();
$(document).on("click", "button.delete", function() {
  var todolist = JSON.parse(localStorage.getItem("todolist"));
  var currentIndex = $(this).attr("data-index");
  // deletes items
  todolist.splice(currentIndex, 1);
  list = todolist;
  localStorage.setItem("todolist", JSON.stringify(todolist));
  putOnPage();
});
$("input[type='submit']").on("click", function(event) {
  event.preventDefault();
  // changes input to variable and clears input field
  var val = $("input[type='text']").val();
  $("input[type='text']").val("");
  // adds to do to list and local storage
  list.push(val);
  localStorage.setItem("todolist", JSON.stringify(list));
  putOnPage();
});

1 个答案:

答案 0 :(得分:1)

您可能可以使列表成为具有三个对象的数组,每个对象包含一个与页面分开的列,然后使用list[0]表示列表1,list[1]表示列表2和等等。

您可以通过在本地存储中使用三个存储来解决此问题,每一列一个。

JS小提琴示例-> https://jsfiddle.net/jassMarok/3gcnyamp/18/

// find elements
var banner = $("#banner-message")
var button = $("button")
var testObject = [{
    name: "todo-list1",
    items: ["Sell", "Buy", "Donate"]
  },
  {
    name: "todo-list2",
    items: ["Go", "Run", "Sleep"]
  },
  {
    name: "todo-list3",
    items: ["Work", "Program", "Code"]
  }
];
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));

// handle click and add class
button.on("click", function() {

  var lists = $('[id|="list"]');
  console.log(lists);
  var retrievedObject = JSON.parse(localStorage.getItem('testObject'));
  console.log(retrievedObject)

  for (i = 0; i < lists.length; i++) {
    var $list = lists[i];
    var items = retrievedObject[i].items;
    for (j = 0; j < items.length; j++) {
      console.log(items);
      $($list).append('<li><a href="#">' + items[j] + '</a></li>');
    }
  }
})
body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
  transition: all 0.2s;
  margin: 0 auto;
  width: 300px;
}

button {
  background: #0084ff;
  border: none;
  border-radius: 5px;
  padding: 8px 14px;
  font-size: 15px;
  color: #fff;
}

#banner-message.alt {
  background: #0084ff;
  color: #fff;
  margin-top: 40px;
  width: 200px;
}

#banner-message.alt button {
  background: #fff;
  color: #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="banner-message">
  <p>Hello World</p>
  <button>Load Data</button>
  <hr/>
  <h3>
    List 1
  </h3>
  <ul id="list-1"></ul>
  <hr/>
  <h3>
    List 2
  </h3>
  <ul id="list-2"></ul>
  <hr/>
  <h3>
    List 3
  </h3>
  <ul id="list-3"></ul>
</div>