Pagination for JSON results in HTMLJavascript page - Stack Overflow

I'm creating an ONE PAGE WEB APP to display products. Thanks to a portion of javascript code, I co

I'm creating an ONE PAGE WEB APP to display products. Thanks to a portion of javascript code, I collect the data from a json file and display it on the HTML page.

The problem is that each time all the data in the json file is displayed. I would like to have the possibility to add pagination or an infinite scroll to navigate through the results, without all being loaded at the same time.

I tried: /
/

but the documentation is really too plex for me, please ask for a little help or an alternative solution for my situation.

This is the js to fetch data from JSON file

$(function () {
    var Product = [];
    $.getJSON('data/people.json', function (data) {
        $.each(data.Product, function (i, f) {


//...some stuff...

  var card =

//  ...some stuff...

          $(card).appendTo("#list");
        });
    });
});

This is HTML section where result displayed

 <div id="list" class="card-columns mx-4 px-4">

    </div>

JSON


{
    "Product": [
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        },
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        },
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        }
    ]
}

I'm creating an ONE PAGE WEB APP to display products. Thanks to a portion of javascript code, I collect the data from a json file and display it on the HTML page.

The problem is that each time all the data in the json file is displayed. I would like to have the possibility to add pagination or an infinite scroll to navigate through the results, without all being loaded at the same time.

I tried: https://infinite-scroll./
https://pagination.js/

but the documentation is really too plex for me, please ask for a little help or an alternative solution for my situation.

This is the js to fetch data from JSON file

$(function () {
    var Product = [];
    $.getJSON('data/people.json', function (data) {
        $.each(data.Product, function (i, f) {


//...some stuff...

  var card =

//  ...some stuff...

          $(card).appendTo("#list");
        });
    });
});

This is HTML section where result displayed

 <div id="list" class="card-columns mx-4 px-4">

    </div>

JSON


{
    "Product": [
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        },
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        },
        {
            "Key1": "Value1",
            "Key2": "Value2",
            "Key3": "Value3",
            "Key4": "Value4",
            "Key5": "Value5"
        }
    ]
}
Share Improve this question asked Aug 15, 2019 at 15:40 StewStew 951 gold badge2 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

so what you need to do is something like this:

var json = {
    "Product": [
        {
            "Key1": "Value1 A"
        },
        {
            "Key1": "Value1 B"
        },
        {
            "Key1": "Value1 C"
        },
        {
            "Key1": "Value1 D"
        },
        {
            "Key1": "Value1 E"
        },
        {
            "Key1": "Value1 F"
        },
        {
            "Key1": "Value1 G"
        },
        {
            "Key1": "Value1 H"
        },
        {
            "Key1": "Value1 I"
        }
    ]
}

$('#list').pagination({ // you call the plugin
  dataSource: json.Product, // pass all the data
  pageSize: 2, // put how many items per page you want
  callback: function(data, pagination) {
      // data will be chunk of your data (json.Product) per page
      // that you need to display
      var wrapper = $('#list .wrapper').empty();
      $.each(data, function (i, f) {
         $('#list .wrapper').append('<ul><li>Key1: ' + f.Key1 + '</li></ul>');
      });
    }
   });
<div id="list"><div class="wrapper"></div></div>
<script src="https://code.jquery./jquery-3.4.1.min.js"></script>
<script src="https://pagination.js/dist/2.1.4/pagination.min.js"></script>
<link rel="stylesheet" href="https://pagination.js/dist/2.1.4/pagination.css"/>

In your code:

$.getJSON('data/people.json', function (json) {
    $('#list').pagination({
        dataSource: json.Product,
        pageSize: 2,
        callback: function(data, pagination) {
            var wrapper = $('#list .wrapper').empty();
            $.each(data, function (i, f) {
                $('#list .wrapper').append('<ul><li>Key1: ' + f.Key1 + '</li></ul>');
            });
        }
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744807361a4594857.html

相关推荐

  • Pagination for JSON results in HTMLJavascript page - Stack Overflow

    I'm creating an ONE PAGE WEB APP to display products. Thanks to a portion of javascript code, I co

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信