[英]javascript localStorage add to table
For an assignment I need to use javascript/jquery, but I'm new to it. 对于作业,我需要使用javascript / jquery,但我是新手。 I have a form with some text boxes where I can create a new animal.
我有一个带有一些文本框的表单,可以在其中创建新的动物。 The animal will be added to an array called animal_list.
该动物将被添加到名为animal_list的数组中。
Animal_list will be added to localstorage and it works fine. Animal_list将被添加到localstorage,并且可以正常工作。 But what I want is to load all animals from the local storage and add them to a table.
但是我想要从本地存储中加载所有动物并将它们添加到表中。 The type of animals that will be stored are a cat or a dog and they also have their own table.
将要存储的动物是猫还是狗,它们也有自己的桌子。
I have a console.log where I write the type of the animal, and this shows the right output (3 dogs, 2 cats), but they won't be added to a table. 我有一个console.log,其中写入了动物的类型,这显示了正确的输出(3只狗,2只猫),但是不会将它们添加到表中。
This is my HTML code: 这是我的HTML代码:
<div class="row">
<div class="col-lg-6">
<h3>Honden</h3>
<table id="dog" class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>CRN</th>
<th>Name</th>
<th>DoB</th>
<th>Gender</th>
<th>Last Walk Date</th>
</tr>
</thead>
</table>
</div>
<div class="col-lg-6">
<h3>Katten</h3>
<table id="cat" class="table table-hover table-striped table-condensed">
<thead>
<tr>
<th>CRN</th>
<th>Name</th>
<th>DoB</th>
<th>Gender</th>
<th>Bad Habits</th>
</tr>
</thead>
</table>
</div>
</div>
And this is my javascript/jQuery code: 这是我的javascript / jQuery代码:
$( document ).ready(function(){
/* Eerst alle animals ophalen */
var animal_list = JSON.parse(localStorage.getItem("animals"));
if (Array.isArray(animal_list)) {
for (var i = 0; i < animal_list.length; ++i)
{
/* get a specific animal from our array so we can use it's properties */
var animal = animal_list[i];
console.log("Loading next animal...");
console.log("Animal type: " + animal.Type);
if(animal.Type == "dog")
{
// animal is a dog
var table = document.getElementById("#dog");
row = table.insertRow(i+1);
Crn = row.insertCell(0);
Name = row.insertCell(1);
DoB = row.insertCell(2);
Gender = row.insertCell(3);
Special = row.insertCell(4);
Crn.innerHTML(animal.Crn);
Name.innerHTML(animal.Name);
DoB.innerHTML(animal.Dob);
Gender.innerHTML(animal.gender);
Special.innerHTML(animal.Special);
save(Crn.innerHTML);
save(Name.innerHTML);
save(DoB.innerHTML);
save(Gender.innerHTML);
save(Special.innerHTML);
}
if(animal.Type == "cat")
{
// animal is a cat
var table = document.getElementById("#cat");
row = table.insertRow(i+1);
Crn = row.insertCell(0);
Name = row.insertCell(1);
DoB = row.insertCell(2);
Gender = row.insertCell(3);
Special = row.insertCell(4);
Crn.innerHTML(animal.Crn);
Name.innerHTML(animal.Name);
DoB.innerHTML(animal.Dob);
Gender.innerHTML(animal.gender);
Special.innerHTML(animal.Special);
save(Crn.innerHTML);
save(Name.innerHTML);
save(DoB.innerHTML);
save(Gender.innerHTML);
save(Special.innerHTML);
}
}
}
});
The console in safari gives no error messages. Safari浏览器中的控制台未显示任何错误消息。 I also tried to change
我也尝试改变
document.getElementById("#dog");
to (also for cat) 到(也用于猫)
document.getElementById("dog");
but with no result. 但没有结果。
Could someone help me with my problem? 有人可以帮我解决我的问题吗?
Thanks in advance! 提前致谢!
EDIT: I fount a typo (animal.type should be animal.Type). 编辑:我发现一个错字(animal.type应该是animal.Type)。 When changed that I got an error message in my console:
更改后,我在控制台中收到一条错误消息:
TypeError: null is not an object (evaluating 'table.insertRow')
TypeError:null不是对象(正在评估“ table.insertRow”)
(typo fixed in code) (打字错误已在代码中修正)
I used jQuery to add the objects in the localStorage to a table. 我使用jQuery将localStorage中的对象添加到表中。
This is an example for the dog-table 这是狗桌的一个例子
if(animal.Type == "dog")
{
// animal is a dog
var table = document.getElementById("dog");
$("#dog").append("<tr><td>" + animal.Crn + "</td><td>" + animal.Name + "</td><td>" + animal.Dob +"</td><td>" + animal.Gender + "</td><td>" + animal.Special + "</td></tr>");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.