Javascript / Jquery:如何对JSON对象进行分页?

时间:2017-11-19 04:38:08

标签: javascript jquery json pagination

我想知道为我的场景添加分页的合适方法:我使用" $(' body')。append(htmlText);"显示JSON对象中的项目。如何为每个页面添加一个显示1个项目的分页,具体取决于JSON中有多少个?

<!DOCTYPE html>
<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

</head>

<body>

<script>
    var data = [{
                 "name": "Bebe",
                 "lastname": "Rexha",
                 "description": "Young and beautiful",
                 "producer": "ABC Music",
                 "users_name": "BebeLove",
                },
                {
                  "name": "Katy",
                  "lastname": "Perry",
                  "description": "Cute and sweet",
                  "producer": "Kitty Music",
                  "users_name": "KittyCat",
                },
               ]

    var htmlText = '';

    for ( var key in data ) {
        htmlText += '<div class="div-conatiner">';
        htmlText += '<p class="p-name"> Name: ' + data[key].name + '</p>';
        htmlText += '<p class="p-loc"> LastName: ' + data[key].lastname + '</p>';
        htmlText += '<p class="p-desc"> Description: ' + data[key].description + '</p>';
        htmlText += '<p class="p-created"> Produced by: ' + data[key].producer + '</p>';
        htmlText += '<p class="p-uname"> Username: ' + data[key].users_name + '</p>';
        htmlText += '</div>';
    }

    $('body').append(htmlText);

</script>

</body>
</html>

谢谢。欢呼:)

1 个答案:

答案 0 :(得分:1)

执行此操作的最佳方法是使用Array.forEach()