Показать сообщение отдельно
  #2 (permalink)  
Старый 20.10.2019, 22:33
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,068

создание таблицы
Potatka,
как вариант ...
<!doctype html>

<meta charset="utf8">
<style>
    /* Defines a cleaner look for tables */
    table  { border-collapse: collapse; }
    td, th { border: 1px solid black; padding: 3px 8px; }
    th     { text-align: left; }
    tbody td:nth-child(2) {
        text-align: right;
    }

</style>


<h1>Mountains</h1>

<div id="mountains"></div>

<script>

    const MOUNTAINS = [
        {name: "Kilimanjaro", height: 5895, place: "Tanzania"},
        {name: "Everest", height: 8848, place: "Nepal"},
        {name: "Mount Fuji", height: 3776, place: "Japan"},
        {name: "Vaalserberg", height: 323, place: "Netherlands"},
        {name: "Denali", height: 6168, place: "United States"},
        {name: "Popocatepetl", height: 5465, place: "Mexico"},
        {name: "Mont Blanc", height: 4808, place: "Italy/France"}
    ];

    const createTr = (arr, tag = 'td') => `<tr><${tag}>` + arr.join(`<${tag}>`);

    function buildTable(data) {
        let table = document.createElement('table');
        let fields = Object.keys(data[0]);
        let trs = data.map(obj => createTr(Object.values(obj))).join('');
        let html = '<thead>' + createTr(fields, 'th') + '<tbody>' + trs;
        table.innerHTML = html;
        return table;
    }

    document.querySelector("#mountains").appendChild(buildTable(MOUNTAINS));
</script>

Последний раз редактировалось рони, 21.10.2019 в 16:37.
Ответить с цитированием