Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
1
Indexable
Never
export default class UserTable {
  constructor(rows) {
    this.rows = rows;
    this.elem = null;
    this.createTable();
  }

  tHeadName = { // tHeadName.name
    name: 'Имя',
    age: 'Возраст',
    salary: 'Зарплата',
    city: 'Город'
  }

  createRow() {
    return (
      this.rows.map(el => {
        const tr = document.createElement('tr');
        this.createCells(el).forEach(cell => {
          tr.append(cell);
        });

        return tr;
      })
    );
  }

  cell = document.createElement(td);
  buttonRemove = document.createElement('button');

  createCells(person) {

    const cells = Object.values(person).map(el => {
      const td = document.createElement('td');
      td.append(el);

      return td;
    });


    // cells.push();

  }

  createTable() {
    const table = document.createElement('table');
    const tHead = document.createElement('thead');
    const tr = document.createElement('tr');

    const tBody = document.createElement('tbody');
    table.append(tHead);


    Object.keys(this.rows[0]).forEach(el => {
      const th = document.createElement('th');
      th.innerHTML = this.tHeadName[el];
      tr.append(th);
    });

    tHead.append(tr);
    const row = this.createRow().forEach(row => tBody.append(row));
    table.append(tBody);
    this.elem = table;
  }
}