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

vandalv,
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.3.0/lodash.js"></script>
    <style>
        table {
            border-collapse: collapse;
        }

        td {
            border: 1px solid gray;
            padding: 2px 5px;
            text-align: center;
        }
    </style>
</head>

<body>

    <div id="grid-holder"></div>

    <script type="text/template" id="grid-template">
        <table>
            <thead>
                <tr>
                    <th>№</th>
                    <th>сумма</th>
                    <th>продукты</th>
                </tr>
            </thead>
            <tbody>
                <% for(var i = 0; i < list.length; i++) { %>
                    <tr><td>
                            <%=i + 1%>
                        </td>
                        <td>
                            <%=list[i].total%>
                        </td>
                        <td><ul>
                        <% for(var k = 0; k < list[i].produkt.length; k++) { %>
                        <li>
                            <%=list[i].produkt[k].name%>
                         </li>
                        <% } %>
                            </ul>
                        </td>
                    </tr>
                    <% } %>
            </tbody>
        </table>
    </script>

    <script>
function randomValueBetween(min, max) {
        return Math.random() * (max - min) + min;
}
function randomDate(date1 = '01-01-2017', date2 = new Date().toLocaleDateString()) {
    date1 = new Date(date1).getTime();
    date2 = new Date(date2).getTime();
    return new Date(randomValueBetween(date2, date1)).toLocaleDateString()
}
function getElem(arr)
{
    return arr[randomValueBetween(0, arr.length)|0]
}
var vegString=["Apple","Apricots","Avocado","Banana","Blackberries"];
    var bases = Array.from({length : 10}, _ => {
    var total = 0,
    produkt = Array.from({length : 5}, _ => {
    var num = randomValueBetween(100, 999)|0,
            name = getElem(vegString),
            price = randomValueBetween(1, 100).toFixed(2),
            count = randomValueBetween(1, 100)|0;
            total += price * count;
            return {num, name, price, count}
    });
    total = total.toFixed(2);
    return {total, produkt}
    })




        var tmpl = document.getElementById('grid-template').innerHTML.trim();
        tmpl = _.template(tmpl);


        document.getElementById('grid-holder').insertAdjacentHTML('beforeEnd' ,tmpl({
            list: bases
        }));
        document.getElementById('grid-holder').insertAdjacentHTML('beforeEnd' , '<h1>5  фактур по найбольшей стоимости</h1>');
        var sortTotal = bases.sort((a,b)=> a.total - b.total).slice(-5);
        document.getElementById('grid-holder').insertAdjacentHTML('beforeEnd' ,tmpl({
            list: sortTotal
        }));
        document.getElementById('grid-holder').insertAdjacentHTML('beforeEnd' , '<h1>фактуры по возрастанию стоимости, в которых есть Avocado</h1>');
        var filterAvocadoSortTotal = bases.filter(base => base.produkt.some(el  => el.name == 'Avocado')).sort((a,b)=> a.total - b.total);
        document.getElementById('grid-holder').insertAdjacentHTML('beforeEnd' ,tmpl({
            list: filterAvocadoSortTotal
        }));

    </script>

</body>

</html>
Ответить с цитированием