0

PAGINATION DEMO

I am developing a pagination with handlebars.js & simplePagination.js

  1. Data is loaded from JSON and displayed using handlebars.js

  2. Now I have to Map JSON DATA TO PAGINATION, where do I do it? Apart from this there is not Much Documentation in the official website "http://flaviusmatis.github.io/simplePagination.js"

Request your kind help. Any advice / suggestions are greatly appreciated.

JS CODE :

$(function () {
    function loadPosts(posts) {
        $('#posts').empty();
        posts.each(function () {
            var source = $("#post-template").html();
            var template = Handlebars.compile(source);
             var context = {
            author: this.data.author, 
            domain: this.data.domain,
            id: this.data.id,
            ups: this.data.ups,
            downs: this.data.downs,
            num_comments: this.data.num_comments,
            subreddit: this.data.subreddit,
            title: this.data.title,
            url: this.data.url,
            permalink: this.data.permalink,
        };
            var html = template(context);
            $('#posts').append(html);
        });
    }
    $.ajax({
        dataType: 'json',
        url: "https://www.reddit.com/.json",
        success: function (response_json) {
           // data = $(response_json.records.page);
          data = $(response_json.data.children);
            dataCount = data.length;
            //console.log(data);
            /* if (dataCount > opts.pageMax) {
                 paginate(pageCount);
                 posts = data.slice(0, opts.pageMax);
                 console.log(posts)
             } else {
                 posts = data;
             }*/
            //loadPosts(posts);
            loadPosts(data);

            $('.pagination').pagination({
                items: dataCount, // json length count
                itemsOnPage: 4
            });
            $('.pagination').pagination('selectPage', 1);
            //$('.pagination').pagination('prevPage', dataCount);
            //$('.pagination').pagination('nextPage', dataCount);
            //$('.pagination').pagination('getPagesCount', dataCount);
            //$('.pagination').pagination('getCurrentPage', dataCount);
            $('.pagination').pagination('updateItems', dataCount);


        }
    });

});
1
  • I think the pagination is happening here if (dataCount > opts.pageMax) { paginate(pageCount); posts = data.slice(0, opts.pageMax); console.log(posts) } else { posts = data; } !! Commented Jul 20, 2017 at 8:46

1 Answer 1

1

Before setting up pagination, just hide other items items.slice(4).hide();

 $("#light-pagination").pagination({
        items: 25,
        itemsOnPage: 4,
        cssStyle: "light-theme",
        // This is the actual page changing functionality.
        onPageClick: function(pageNumber) {
          // We need to show and hide `tr`s appropriately.
          var showFrom = 4 * (pageNumber - 1);
          var showTo = showFrom + 4;

          items.hide()
            .slice(showFrom, showTo).show();
        }
      });

Working example here below

$(function() {
  function loadPosts(posts) {
    $('#posts').empty();
    posts.each(function() {
      var source = $("#post-template").html();
      var template = Handlebars.compile(source);
      var context = {
        author: this.data.author,
        domain: this.data.domain,
        id: this.data.id,
        ups: this.data.ups,
        downs: this.data.downs,
        num_comments: this.data.num_comments,
        subreddit: this.data.subreddit,
        title: this.data.title,
        url: this.data.url,
        permalink: this.data.permalink,
      };
      var html = template(context);
      $('#posts').append(html);
    });
  }
  $.ajax({
    dataType: 'json',
    url: "https://www.reddit.com/.json",
    success: function(response_json) {
      // data = $(response_json.records.page);
      data = $(response_json.data.children);
      dataCount = data.length;

      loadPosts(data);
      var items = $(".score-right");
      items.slice(4).hide();

      $("#light-pagination").pagination({
        items: 25,
        itemsOnPage: 4,
        cssStyle: "light-theme",


        // This is the actual page changing functionality.
        onPageClick: function(pageNumber) {
          // We need to show and hide `tr`s appropriately.
          var showFrom = 4 * (pageNumber - 1);
          var showTo = showFrom + 4;

          // We'll first hide everything...
          items.hide()
            // ... and then only show the appropriate rows.
            .slice(showFrom, showTo).show();
        }
      });


    }
  });


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
<script src="https://flaviusmatis.github.io/simplePagination.js/jquery.simplePagination.js"></script>
<link href="https://flaviusmatis.github.io/simplePagination.js/simplePagination.css" rel="stylesheet">

<div id="light-pagination" class="pagination light-theme simple-pagination"></div>
<div id="posts"></div>

<script id="post-template" type="text/x-handlebars-template">
  <div class="score-structural score-column2-wideright search-listings post">
    <div class="score-right">
      <h4>{{record_count}}</h4>
      <h5 style="z-index: 1;">
        <a href="#"> {{ title }} </a>
      </h5>
      <p style="z-index: 1;"> {{ desc }} </p>
    </div>
  </div>
</script>

You need to hide initial data

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a TON Yogen !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.