【问题标题】:How to set up writing to html table from .json file如何设置从 .json 文件写入 html 表
【发布时间】:2021-12-27 09:19:21
【问题描述】:

我需要编写这样的代码,该代码将从仅包含 .json 文件的指定文件夹(轨道)中填充表格,例如:

{
            "EndTime": "11:00",
            "Person": [
                {
                    "name": "one",
                    "age": 5
                },
                {
                    "name": "two",
                    "age": 7
                }
            ],
            "StartTime": "10:45"
        }

我是初学者,我不明白如何将行附加到表中,从 .json 文件中获取数据 html.file

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MUI Landing Page Example</title>
    <link href="https://cdn.muicss.com/mui-latest/css/mui.min.css" rel="stylesheet" type="text/css" />
    <title>Measurements</title>
    <script src="https://cdn.muicss.com/mui-latest/js/mui.min.js"></script>
  </head>
  <body>
    <header class="mui-appbar mui--z1">
      <div class="mui-container">
        <table>
          <tr class="mui--appbar-height">
            <td class="mui--text-title">Measurements</td>
          </tr>
        </table>
      </div>
    </header>
    <div id="content-wrapper" class="mui--text-center">
      <div class="mui--appbar-height"></div>
      <br>
      <br>
      <div class="mui--text-display3">Measurements</div>
      <br>
      <br>
      <button 
        type = "submit"
        id = "submit"
        class="mui-btn mui-btn--primary"
        >
        take info
    </button>
    </div>
    <table class="mui-table">
        <thead>
          <tr>
            <th>N</th>
            <th>Name of file</th>
            <th>Start time</th>
            <th>End time</th>
            <th>Nuber of person</th>
          </tr>
        </thead>
        <tbody>
        </tbody id='myTable'>
      </table>
    <footer>
      <div class="mui-container mui--text-center">
        Made by Kumiho
      </div>
    </footer>
  </body>
</html>

webpack.config.js

const path = require('path')
const HTMLPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
    entry: './src/app.js',
    output : {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'public')
    },
    devServer: {
        port: 3000
    },
    plugins: [
new HTMLPlugin({
template: './src/index.html'
}),
new CleanWebpackPlugin()
    ],
    module: {
        rules: [
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
          {
            test: /\.m?js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env']
              }
            }
          }
        ],
      }
}

app.js

import './static/styles.css'

const table = document.getElementById('myTable')
document.querySelector("#submit").onclick = function(){
        const tr = [{
            "EndTime": "11:00",
            "Person": [
                {
                    "name": "one",
                    "age": 5
                },
                {
                    "name": "two",
                    "age": 7
                }
            ],
            "StartTime": "10:45"
        },
        {
            "EndTime": "11:05",
            "Person": [
                {
                    "name": "three",
                    "age": 6
                },
                {
                    "name": "four",
                    "age": 7
                }
            ],
            "StartTime": "11:00"
        }]
document.querySelector('.content').insertRow= `<table class="track"></table>`


   for (let i=0; i< tr.length; i++) {
    let tr = document.createElement('TR');
    let td = document.createElement('TD');
    document.querySelector("#submit").onclick = function(){

    let row = document.createElement ('str')
    row.insertRow = '<tr>' +
                  `<td>+${index}+</td>` +
                  `<td>+${file}+</td>` +
                  `<td>+${tr.StartTime}+</td>` +
                  `<td>+${tr.EndTime}+</td>` +
                  `<td>+${tr.Person.length}+</td>` +
                '</tr>';
   
   document.querySelector('.track').appendChild(row)
    
}
}

网站启动,但报错:

app.js:28 Uncaught TypeError: Cannot set properties of null (setting 'insertRow') at HTMLButtonElement.document.querySelector.onclick

此外,如您所见,即使给定的对象数组也不会填满表格。

【问题讨论】:

    标签: javascript html node.js json webpack


    【解决方案1】:

    问题太多了。

    这是一个工作版本 - 我将列出一些问题

    1. 永远不要调用任何提交。如果在表单中,提交事件将被隐藏
    2. 您没有有效地使用模板文字。不要连接模板文字 - 这是对文字的浪费
    3. 您想要填充表格,但您尝试设置内容的 insertRow 属性
    4. let row = document.createElement ('str') 应该做什么? &lt;str&gt; 不是已知的 HTML 标记

    我没有看其他代码 - 问更多问题

    const rows = [{
        "EndTime": "11:00",
        "Person": [{
            "name": "one",
            "age": 5
          },
          {
            "name": "two",
            "age": 7
          }
        ],
        "StartTime": "10:45"
      },
      {
        "EndTime": "11:05",
        "Person": [{
            "name": "three",
            "age": 6
          },
          {
            "name": "four",
            "age": 7
          }
        ],
        "StartTime": "11:00"
      }
    ]
    
    const table = document.getElementById('myTable')
    document.getElementById("subBut").addEventListener("click", function(e) {
      e.preventDefault(); 
      table.innerHTML = rows.map((tr, index) => `<tr><td>${index}</td>
              <td>${tr.StartTime}</td>
              <td>${tr.EndTime}</td>
              <td>${tr.Person.length}</td>
              </tr>`).join("")
    })
    <table>
      <tbody id="myTable" class="track"></tbody>
    </table>
    <input type="submit" id="subBut" />

    【讨论】:

    • 感谢您的建议和您的工作。现在的错误是 > app.js: 31 Uncaught TypeError: Cannot set properties of null (setting 'innerHTML') 请帮我解决这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 2019-10-06
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多