Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can whole table be dinamically created in ejs?

Controller looks like this:

'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

I have APIs that return JSON list of objects that differ in number of columns.
Can I create single EJS page that would generate table based on the number of columns received?
For example, one JSON would return:

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

and another would have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ {
        "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
    }, ...
]

So, can I use EJS to dynamically create table depending on the JSON received? A link or example would be great, but explanation is preferred!

I know how to iterate over an array, but only if I know the names of the field (columns), like this:

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

but do not know how to do it if I have no information on the number or names of the fields.

>Solution :

you can iterate over the array like you said, then iterate over each object of the array twice. First to print the headers, then to print the data

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading