可以在ejs中动态创建整个table吗?

Can whole table be dynamically created in ejs?

控制器看起来像这样:

'use strict';

const PostgreRepository = require('../repository/repository');
const sequelize = require('../config/database');
const models = require('../models/init-models')

models(sequelize);

class Controller {
    constructor(model) {
        this._model = model;
        this.add = this.add.bind(this);
        this.findAll = this.findAll.bind(this);
        this.findOne = this.findOne.bind(this);
        this.update = this.update.bind(this);
        this.remove = this.remove.bind(this);
        this.repo = new PostgreRepository(sequelize, this._model)
    }

    add(req, res) {
        console.log(req.originalUrl);
        this.repo.add(req.body).then(data =>{
            res.send(data)
        }, err => res.status(400).send(err))
    }

    findAll(req, res) {
        this.repo.findAll().then(data => {
            res.send(data)
            // res.render('pages/index', {data:data})
        }, err => res.status(400).send(err))

    }

    findOne(req, res) {
        this.repo.findOne(req.params.id).then(data => {
            res.send(data)
        }, err => res.status(400).send(err))
    }

    update(req, res) {
        this.repo.update(req.body, req.params.id).then(data => {
            res.send(data)
        }, err => res.status(400).send(err))
    }

    remove(req, res) {
        console.log(req.params.id);
        this.repo.remove(req.params.id).then(data => {
            res.send(data)
        }, err => res.status(400).send(err))
    }
}

module.exports = Controller

我的 API return JSON 列数不同的对象列表。

我可以创建单个 EJS 页面,根据收到的列数生成 table 吗?

例如,一个 JSON 将 return:

[
    {
        "country_pk": 1,
        "code": "AF",
        "name": "Afghanistan"
    },
    {
        "country_pk": 2,
        "code": "AL",
        "name": "Albania"
    },
    {
        "country_pk": 3,
        "code": "DZ",
        "name": "Algeria"
    }, ...
]

另一个人会:

[ {
        "city_pk": 1041,
        "city": "Partesh",
        "lat": "42.4019",
        "lng": "21.4336",
        "admin_name": "Partesh",
        "capital": "admin",
        "population": "5300",
        "population_proper": "5300",
        "country": 247
    },
    {
        "city_pk": 1042,
        "city": "Korishë",
        "lat": "42.2576",
        "lng": "20.7981",
        "admin_name": "Prizren",
        "capital": "",
        "population": "5279",
        "population_proper": "5279",
        "country": 247
    }, ...
]

那么,我可以使用 EJS 根据收到的 JSON 动态创建 table 吗? link 或示例会很棒,但最好有解释!

我知道如何遍历数组,但前提是我知道字段(列)的名称,如下所示:

<% for(var i=0; i<data.length; i++){ %>
<h1><%= data[i].code %></h1>
<h3><%= data[i].name %></h3>
<% } %> 

但如果我没有关于字段的数量或名称的信息,我不知道该怎么做。

你可以像你说的那样遍历数组,然后对数组的每个 object 遍历两次。首先打印headers,然后打印数据

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

The for...in statement iterates over all enumerable properties of an object that are keyed by strings

<table>
 <% for(const obj of data){ %>
  <tr>
   <% for(const key in obj) { %>
    <th><%= key %></th>
   <% } %>
  </tr>
  <tr>
  <% for(const key in obj) { %>
   <td><%= obj[key] %></td>
  <% } %>
  </tr>
 <% } %> 
</table>