简体   繁体   English

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

[英]Javascript/Jquery: How to paginate a JSON object?

I would like to know the suitable method to add pagination for my scenario: I using the "$('body').append(htmlText);" 我想知道适合我的情况添加分页的合适方法:我使用“ $('body')。append(htmlText);” to display the items within the JSON object. 以显示JSON对象中的项目。 How to add a pagination that display 1 item for each page depending on how many got in the 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>

Thank you. 谢谢。 Cheer :) 欢呼:)

最好的方法是使用Array.forEach()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM