JavaScript 用于对象的表格

huangapple go评论66阅读模式
英文:

Javascript table using this for objects

问题

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Library</title>
</head>
<body>
    <h1>My Library Book List</h1>
    <p>This is my simple library project using object oriented javascript.</p>
    <p id="myBooks"></p>
    <br>
    <form action="#" method="post">
        <label for="title">Title:</label>
        <input type="text" id="title" name="title"><br>
        <label for="author">Author:</label>
        <input type="text" id="author" name="author"><br>
        <label for="pages">Pages:</label>
        <input type="text" id="pages" name="pages"><br>
        <input type="submit" id="submit" value="Submit">
    </form>
    <script>
        document.getElementById("submit").addEventListener("click", function(event){
            event.preventDefault();
        });
    </script>
    <br>
    <table>
        <tr>
            <th>Title</th>
            <th>Author</th>
            <th>Pages</th>
        </tr>
        <tr>
            <td id="tableTitle"></td>
            <td id="tableAuthor"></td>
            <td id="tablePages"></td>
        </tr>
    </table>
    <br>
    <button onclick="window.location.href='/myForm.html';">New myBooks</button>
    <script src="script.js"></script>
</body>
</html>
const bookArray = [];

function Book(author, title, pages){
    // the constructor
    this.author = author
    this.title = title
    this.pages = pages
    this.updateTable = function() {
        document.getElementById('tableTitle').textContent = this.title;
        document.getElementById('tableAuthor').textContent = this.author;
        document.getElementById('tablePages').textContent = this.pages;
    }
}

function addBookToLibrary(title, author, pages) {
    const book = new Book(author, title, pages);
    bookArray.push(book);
    book.updateTable();
    return bookArray;
}

let store1 = new addBookToLibrary("Cat and hat", "Bob", 150);

I have made changes to your HTML and JavaScript code. In the HTML, I added placeholders for displaying the book data in the table by setting the id attributes for the table cells (<td> elements).

In the JavaScript, I added an updateTable method to the Book constructor. This method updates the content of the table cells with the book's title, author, and pages. When you create a new Book and add it to the bookArray, it will automatically update the table with the book's data.

英文:
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Library&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;My Library Book List&lt;/h1&gt;
&lt;p&gt;This is my simple library project using object oriented javascript.&lt;/p&gt;
&lt;p id=&quot;myBooks&quot;&gt;&lt;/p&gt;
&lt;br&gt;
&lt;form action=&quot;#&quot; method=&quot;post&quot;&gt;
&lt;label for=&quot;title&quot;&gt;Title:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;title&quot; name=&quot;title&quot;&gt;&lt;br&gt;
&lt;label for=&quot;author&quot;&gt;Author:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;author&quot; name=&quot;author&quot;&gt;&lt;br&gt;
&lt;label for=&quot;pages&quot;&gt;Pages:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;pages&quot; name=&quot;pages&quot;&gt;&lt;br&gt;
&lt;input type=&quot;submit&quot; id=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;       
&lt;script&gt;
document.getElementById(&quot;submit&quot;).addEventListener
(&quot;click&quot;, function(event){event.preventDefault()
});
&lt;/script&gt;
&lt;br&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;author&lt;/th&gt;
&lt;th&gt;pages&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;title&quot;&gt;&lt;/td&gt;
&lt;td id=&quot;author&quot;&gt;&lt;/td&gt;
&lt;td id=&quot;pages&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;br&gt;
&lt;button onclick=&quot;window.location.href=&#39;/myForm.html&#39;;&quot;&gt;
New myBooks&lt;/button&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
const bookArray = [
];
function Book(author, title, pages){
// the constructor
this.author = author
this.title = title
this.pages = pages
}
function addBookToLibrary(title, author, pages) {
this.title = title
this.author = author
this.pages = pages
this.myArray = []
this.addArray = function(){
this.myArray.push({ title, author, pages });
return this.myArray
}
this.printArray = function() {
var result = &quot;&quot;;
for ( let key in this.myArray) {
return this.myArray[key]
result =+ this.myArray[key];
}
return result
}
this.toHTML = function() {
document.getElementById(&quot;myBooks&quot;).innerHTML = JSON.stringify(this.myArray);
return JSON.stringify(this.myArray)
}    
this.updateTable = function() {
document.getElementById(&#39;title&#39;).innerHTML = this.title;
}
}
let store1 = new addBookToLibrary(&quot;Cat and hat&quot;, &quot;Bob&quot;, 150);

I am expected the function to updateTable to add table data to my html or need a way to statically display each object of book in my table.

Everything I made with this.parameter has been working up to this point. Can i use 'this' reference to display data in my table? I would prefer the table just populate as soons as a object is made.

答案1

得分: 0

你不一定需要在你的HTML中使用 this 来实现这个目的,你可以在你的JS代码中简单地做到这一点。你可以使用以下代码来实现这个目的。我使用了相同的HTML代码,但修改了使用了 this 关键字。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Library</title>
</head>
<body>

    <h1>My Library Book List</h1>
    <p>This is my simple library project using object-oriented JavaScript.</p>
    <p id="myBooks"></p>

    <br>

    <form action="#" method="post">
        <label for="title">Title:</label>
        <input type="text" id="title" name="title"><br>
        <label for="author">Author:</label>
        <input type="text" id="author" name="author"><br>
        <label for="pages">Pages:</label>
        <input type="text" id="pages" name="pages"><br>
        <input type="submit" id="submit" value="Submit">
    </form>

    <script>
        document.getElementById("submit").addEventListener("click", function(event) {
            const title = document.getElementById("title").value;
            const author = document.getElementById("author").value;
            const pages = document.getElementById("pages").value;

            const book = addBookToLibrary(title, author, pages);

            updateTable();

            event.preventDefault();
        });
    </script>

    <br>

    <table>
        <tr>
            <th>Title</th>
            <th>Author</th>
            <th>Pages</th>
        </tr>
        <tr>
            <td id="title"></td>
            <td id="author"></td>
            <td id="pages"></td>
        </tr>
    </table><br>

    <button onclick="window.location.href='/myForm.html';">
        New myBooks
    </button>

    <script src="script.js"></script>
</body>
</html>

我已经将你提供的代码翻译成中文,如果需要更多帮助,请告诉我。

英文:

You don't necessarily need to use this in your HTML for this purpose, you can do it simply in your JS code. You can use the following code for this purpose. I have used the same HTML code but modified with the use of this keyword.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const bookArray = [];
function Book(author, title, pages) {
this.author = author;
this.title = title;
this.pages = pages;
}
function addBookToLibrary(title, author, pages) {
const book = new Book(author, title, pages);
bookArray.push(book);
return book;
}
function updateTable() {
const table = document.getElementById(&quot;myBooks&quot;);
table.innerHTML = &quot;&quot;;
for (const book of bookArray) {
const row = document.createElement(&quot;tr&quot;);
row.innerHTML = `
&lt;td&gt;${book.title}&lt;/td&gt;
&lt;td&gt;${book.author}&lt;/td&gt;
&lt;td&gt;${book.pages}&lt;/td&gt;
`;
table.appendChild(row);
}
}
document.getElementById(&quot;submit&quot;).addEventListener(&quot;click&quot;, function(event) {
const title = document.getElementById(&quot;title&quot;).value;
const author = document.getElementById(&quot;author&quot;).value;
const pages = document.getElementById(&quot;pages&quot;).value;
const book = addBookToLibrary(title, author, pages);
updateTable();
event.preventDefault();
});
updateTable();

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Library&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;My Library Book List&lt;/h1&gt;
&lt;p&gt;This is my simple library project using object oriented javascript.&lt;/p&gt;
&lt;p id=&quot;myBooks&quot;&gt;&lt;/p&gt;
&lt;br&gt;
&lt;form action=&quot;#&quot; method=&quot;post&quot;&gt;
&lt;label for=&quot;title&quot;&gt;Title:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;title&quot; name=&quot;title&quot;&gt;&lt;br&gt;
&lt;label for=&quot;author&quot;&gt;Author:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;author&quot; name=&quot;author&quot;&gt;&lt;br&gt;
&lt;label for=&quot;pages&quot;&gt;Pages:&lt;/label&gt;
&lt;input type=&quot;text&quot; id=&quot;pages&quot; name=&quot;pages&quot;&gt;&lt;br&gt;
&lt;input type=&quot;submit&quot; id=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;       
&lt;script&gt;
document.getElementById(&quot;submit&quot;).addEventListener
(&quot;click&quot;, function(event){event.preventDefault()
});
&lt;/script&gt;
&lt;br&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;title&lt;/th&gt;
&lt;th&gt;author&lt;/th&gt;
&lt;th&gt;pages&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td id=&quot;title&quot;&gt;&lt;/td&gt;
&lt;td id=&quot;author&quot;&gt;&lt;/td&gt;
&lt;td id=&quot;pages&quot;&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;&lt;br&gt;
&lt;button onclick=&quot;window.location.href=&#39;/myForm.html&#39;;&quot;&gt;
New myBooks&lt;/button&gt;
&lt;script src=&quot;script.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

updateTable();

huangapple
  • 本文由 发表于 2023年6月6日 04:15:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定