Advertisement
ExeQue

Untitled

Sep 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 4.52 KB | None | 0 0
  1. <div class="c xs-12 comments">
  2.             </div>
  3.             <div class="c xs-12">
  4.                 <ul class="comments-pager"></ul>
  5.             </div>
  6.             <script type="application/json" id="comment-data">
  7.                 {{{comments}}}
  8.             </script>
  9.             <script type="text/html" id="comment-template">
  10.                 <div class="comment r">
  11.                     <div class="c s-4 m-2 h-xs">
  12.                         <img class="comment-image" src="{{#func.wwwroot}}Uploads/Images/avatar.png{{/func.wwwroot}}?h=100&w=100">
  13.                     </div>
  14.                     <div class="c xs-12 s-8 m-10">
  15.                         <div class="comment-name">##name##</div>
  16.                         <div class="comment-date">##date##</div>
  17.                         <div class="comment-content">##content##</div>
  18.                     </div>
  19.                 </div>
  20.             </script>
  21.             <script type="text/html" id="pager-template">
  22.                 <li class="pager-entry" data-page="##page##"><span>##content##</span></li>
  23.             </script>
  24.             <script>
  25.                 $(function () {
  26.                     var commentsContainer = $('.comments'),
  27.                         template          = $('#comment-template').html(),
  28.                         pager             = $('.comments-pager'),
  29.                         pagerTemplate     = $('#pager-template').html(),
  30.                         data              = JSON.parse($('#comment-data').html());
  31.  
  32.                     var currentPage = 1,
  33.                         maxPerPage  = 3,
  34.                         pages       = Math.ceil(data.count / maxPerPage);
  35.  
  36.                     function setPage(pageNum) {
  37.                         if (pageNum >= 1 || pageNum <= pages) {
  38.                            currentPage = pageNum;
  39.  
  40.                            update();
  41.                        }
  42.                    }
  43.  
  44.                    function update() {
  45.                        pager.find('.pager-entry').each(function () {
  46.                            if (parseInt($(this).attr('data-page')) === currentPage) {
  47.                                $(this).addClass('active');
  48.                            } else {
  49.                                $(this).removeClass('active');
  50.                            }
  51.                        });
  52.  
  53.                        var startIndex = maxPerPage * (currentPage - 1);
  54.  
  55.                        commentsContainer.html('');
  56.                        for (var i = startIndex; i < startIndex + maxPerPage; i++) {
  57.                            var entry = data.entries[i];
  58.                            var temp = JSON.parse(JSON.stringify(template));
  59.                            temp = temp.replace('##name##', entry.name);
  60.                            temp = temp.replace('##date##', entry.added);
  61.                            temp = temp.replace('##content##', entry.content);
  62.                            temp = $(temp);
  63.  
  64.                            commentsContainer.append(temp);
  65.                        }
  66.                    }
  67.  
  68.                    function addPagerEntry(content, pageNum) {
  69.                        var temp = JSON.parse(JSON.stringify(pagerTemplate));
  70.                        temp = temp.replace('##page##', pageNum);
  71.                        temp = temp.replace('##content##', content);
  72.                        temp = $(temp);
  73.  
  74.                        temp.bind('click', function () {
  75.                            if (pageNum === 'prev') {
  76.                                if (currentPage > 1) {
  77.                                     setPage(currentPage - 1);
  78.                                 }
  79.                             } else if (pageNum === 'next') {
  80.                                 if (currentPage < pages) {
  81.                                    setPage(currentPage + 1);
  82.                                }
  83.                            } else {
  84.                                setPage(pageNum);
  85.                            }
  86.                        });
  87.  
  88.                        pager.append(temp);
  89.                    }
  90.  
  91.                    (function init() {
  92.                        addPagerEntry('<i class="fa fa-chevron-left"></i>', 'prev');
  93.                         for (var i = 1; i <= pages; i++) {
  94.                            addPagerEntry(i, i);
  95.                        }
  96.                        addPagerEntry('<i class="fa fa-chevron-right"></i>', 'next');
  97.  
  98.                         update();
  99.                     })();
  100.  
  101.                     console.log(data);
  102.                 });
  103.             </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement