简体   繁体   English

使用 JS 从数组 object 填充表

[英]Populate a table from an array object with JS

I have to make a library app which takes an input from user and stores it in a table on a webpage.我必须制作一个图书馆应用程序,它接受用户的输入并将其存储在网页上的表格中。 Been banging my head at this problem for two days, mostly because I'm a beginner and don't understand most advanced code posted, so i'd even ask you if you'd please make your code very beginner friendly.两天来一直在努力解决这个问题,主要是因为我是初学者并且不理解发布的大多数高级代码,所以我什至会问你是否可以让你的代码对初学者非常友好。

So, i have a form which takes inputs for the author, title, pages, year, a checkbox whether the book's been read, and a button to delete the book.所以,我有一个表单,可以输入作者、标题、页数、年份、是否已阅读书籍的复选框以及删除书籍的按钮。 id = author, title, pages, year, read, respectively, and a class for deleteBtn. id = author、title、pages、year、read,分别是一个 class 用于 deleteBtn。

and a table template in html with only the header, with the intention of populating it with JS:和 html 中的表格模板,只有 header,目的是用 JS 填充它:

<div id="wrapperDiv">
    <table id="table">
        <thead>
            <th class=titleTh>Title:</th>
            <th>Author:</th>
            <th>Pages:</th>
            <th>Year:</th>
            <th>Read status:</th>
            <th>Edit:</th>
        </thead>
        <tbody id="tbody">    
            <!--add with javascript-->
        </tbody>
    </table>
</div>

and with JS, i have this:和 JS,我有这个:

//book array
let books = [];

//saving user input in Object
const addBook =(ev)=>{
ev.preventDefault();

let book = {
    title:document.getElementById("title").value, //.value gives what the user has written
    author:document.getElementById("author").value,
    pages:document.getElementById("pages").value,
    year:document.getElementById("year").value,
    read:document.getElementById("read").checked,
    //edit:document.getElementById("edit").value, 
}
console.log(book);

books.push(book);
document.forms[0].reset(); //clear form for next input
//document.querySelector("form").reset(); same thing
console.log("pushed book")
console.log(books);

localStorage.setItem("BookList", JSON.stringify(books));
//var tbody = document.getElementById("tbody");
var row = document.createElement("tr");
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
var td4 = document.createElement("td");
var td5 = document.createElement("td");  
td5.classList.add("checkTd");
var td6 = document.createElement("button");
td6.innerHTML = "Delete";
td6.classList.add("deleteBtn");

td1.innerHTML = document.getElementById("title").value;
td2.innerHTML = document.getElementById("author").value;
td3.innerHTML = document.getElementById("pages").value;
td4.innerHTML = document.getElementById("year").value;
td5.innerHTML = document.getElementById("read").checked;

row.appendChild(td1);
row.appendChild(td2);
row.appendChild(td3);
row.appendChild(td4);
row.appendChild(td5);
row.appendChild(td6);

table.children[0].appendChild(row);  
} 

document.addEventListener("DOMContentLoaded", ()=>{
document.getElementById("submitBook").addEventListener("click", addBook);
})

So when i input the fields, array gets made, like this:因此,当我输入字段时,会生成数组,如下所示:

[0] Object { [0] Object {

title: "game of thrones"作品名称:《权力的游戏》

author: "george rr martin"作者:“乔治·rr·马丁”

pages: "8000"页数:“8000”

year: "2005"年份:“2005”

read: true读:真

} }

A visual row in the table also gets made, however it's always empty.表格中的可视行也会生成,但它始终是空的。 Also the checkbox is always "false" on the webpage (even if true in array) no matter what kind of if else statement i write.此外,无论我编写哪种 if else 语句,网页上的复选框始终为“假”(即使数组中为真)。 Please help.请帮忙。

If I am right according to your requirements, you can map over the array of books and return a row element for each book, finally, with the rows array, you can append it to the body.如果根据您的要求我是对的,您可以 map 覆盖书籍数组并为每本书返回一个行元素,最后,使用行数组,您可以将 append 放到正文中。

 let books = [{ title: 'Book 1', //.value gives what the user has written author: 'Author 1', pages: '10', year: 2020, read: false, }, { title: 'Book 2', //.value gives what the user has written author: 'Author 2', pages: '10', year: 2021, read: false, }]; let tbody = document.getElementById("tbody"); books.forEach((book) => { let row = document.createElement("tr"); let td1 = document.createElement("td"); let td2 = document.createElement("td"); let td3 = document.createElement("td"); let td4 = document.createElement("td"); let td5 = document.createElement("td"); td5.classList.add("checkTd"); var td6 = document.createElement("button"); td6.innerHTML = "Delete"; td6.classList.add("deleteBtn"); td1.innerHTML = book.title td2.innerHTML = book.author; td3.innerHTML = book.pages; td4.innerHTML = book.year; td5.innerHTML = book.read; row.appendChild(td1); row.appendChild(td2); row.appendChild(td3); row.appendChild(td4); row.appendChild(td5); row.appendChild(td6); // append the row element tbody.appendChild(row); });
 <div id="wrapperDiv"> <table id="table"> <thead> <th class=titleTh>Title:</th> <th>Author:</th> <th>Pages:</th> <th>Year:</th> <th>Read status:</th> <th>Edit:</th> </thead> <tbody id="tbody"> <!--add with javascript--> </tbody> </table> </div>

This might not be the very ideal solution as per readability and performance, should suffice your needs.根据可读性和性能,这可能不是非常理想的解决方案,应该足以满足您的需求。

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

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