Languages
[Edit]
EN

JavaScript - simple pagination / pager component example in Vanilla JS

6 points
Created by:
JustMike
3533

In this article we are going to show how to create simple pagination component with pure JavaScript.

Let's suppose we have few variables that describe pages:

  • pageNumber - current pageĀ number counted from 1 to upĀ (1, 2, 3, ...),
  • pagesCount - total number of pages,
  • radiusSize - maximal number of buttons visible on both sides of active page button.
Custom Vanilla JS pager component example.
Custom Vanilla JS pager component example.

Practical example

As radius, we understand number of items displayed on left and right site of current page button.

Note: using component, replaceĀ /path/to/page/... with correct url.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style>

    div.pager {
    	padding: 20px 0;
    }
    
    a.link { 
    	padding: 5px 10px;
      	text-decoration: none;
      	color: #007bff;
    }

    a.button { 
      	padding: 5px 10px;
      	border: 1px solid;
      	border-radius: 3px;
      	text-decoration: none;
    }
    
    a.button + a.button {
    	margin-left: 5px;
    }
    
    a.button.default {
      	border-color: #66b0ff;
    	background-color: white;
      	color: #007bff;
    }
    
    a.button.default:hover {
    	border-color: #007bff;
        background-color: #e0efff;
    }
    
    a.button.selected {
    	border-color: #007bff;
        background-color: #007bff;
      	color: white;
    }

  </style>
</head>
<body>
  <div id="pager-container"></div>
  <script>
    
    function calculateRange(pageNumber, pagesCount, radiusSize) {
        var start = pageNumber - radiusSize;
        if (start < 1) {
            return {
                start: 1,
                stop: Math.min(2 * radiusSize + 1, pagesCount),
            };
        }
        var stop = pageNumber + radiusSize;
        if (stop > pagesCount) {
            return {
                start: Math.max(1, pagesCount - 2 * radiusSize),
                stop: pagesCount,
            };
        }
        return { start, stop };
    }
    
    function renderPager(pageNumber, pagesCount, radiusSize) {
      	var pager = '';
    	var range = calculateRange(pageNumber, pagesCount, radiusSize);
      	pager += '<div class="pager">';
      	pager +=	'<a class="link" href="/path/to/page/1">First</a>';
      	for (var i = range.start; i <= range.stop; ++i) {
          	var buttonClass = 'button ' + (i === pageNumber ? 'selected' : 'default');
          	var pagePath = '/path/to/page/' + i;
        	pager += '<a class="' + buttonClass + '" href="' + pagePath + '">' +
                        i +
                     '</a>'
        }
      	pager += '<a class="link" href="/path/to/page/' + pagesCount + '">Last</a>';
      	pager += '</div>';
      	return pager;
    }
    
    // Usage example:
    
    var container = document.querySelector('#pager-container');

    container.innerHTML += renderPager(1, 6, 2);
	container.innerHTML += renderPager(3, 6, 2);
	container.innerHTML += renderPager(4, 6, 2);
	container.innerHTML += renderPager(6, 6, 2);

  </script>
</body>
</html>

Ā 

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.