npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

basicpaginate

v1.0.3

Published

Paginate a NodeList like there's no tomorrow

Downloads

2

Readme

basicPaginate

Donate via PayPal

Paginate a NodeList like there's no tomorrow.

basicPaginate turns a list of elements into a JS-controlled pagination.

Contents

Demos

| Name | Description | Link | |:-----------|:------------|:------------| | Default | Includes most features. | Try it on CodePen |

Features

  • Works in all modern browsers and IE11 (with polyfills)
  • Supports all types of DOM elements
  • Doesn't force you to use specific classes or markup
  • CommonJS and AMD support
  • Simple JS API

Requirements

basicPaginate depends on the following browser features and APIs:

Some of these APIs are capable of being polyfilled in older browsers. Check the linked resources above to determine if you must polyfill to achieve your desired level of browser support.

Setup

We recommend installing basicPaginate using npm or yarn.

npm install basicpaginate
yarn add basicpaginate

Include the JS file at the end of your body tag…

<script src="dist/basicPaginate.min.js"></script>

…or skip the JS file and use basicPaginate as a module:

const basicPaginate = require('basicpaginate')
import * as basicPaginate from 'basicpaginate'

Usage

This demo shows how to use basicPaginate to turn a bunch of elements into a paginated list.

<!-- Elements that should be paginated -->
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
<div class="item">Item 4</div>
<div class="item">Item 5</div>
<div class="item">Item 6</div>
<div class="item">Item 7</div>
<div class="item">Item 8</div>

<!-- Placeholder for the pagination -->
<div class="placeholder"></div>
// 1) Create a new pagination with the items and show up to 4 elements per page
const instance = basicPaginate.create(document.querySelectorAll('.item'), 4)

// 2) Use the `render` function to generate the HTML and to render it to the DOM
instance.render((instance) => {

	const placeholder = document.querySelector('.placeholder')

	// 3) Generate the HTML of your pagination
	// Note: It doesn't matter how you generate the HTML as basicPaginate works with any structure
	placeholder.innerHTML = `
		<div class="pagination">
			<button data-basicpaginate-prev>←</button>
			<button data-basicpaginate-next>→</button>
		</div>
	`

	// 4) Return the created element so basicPaginate can look for special attributes
	// Note: You can also bind the event manually without adding attributes to the elements
	return placeholder

})

// 5) Control every aspect of the pagination programmatically
instance.first()
instance.last()
instance.prev()
instance.next()
instance.goto(0)

API

.create(elems, elemsPerPage)

Creates a new basicPaginate instance.

Be sure to assign your instance to a variable. Using your instance, you can…

  • …get the current page index.
  • …navigate back and forward.
  • …goto a specific page.
  • …goto a the first or last page.
  • …tell basicPaginate to render a HTML pagination.

Example:

const instance = basicPaginate.create(document.querySelectorAll('.item'), 4)

Parameters:

  • elems {NodeList} Elements that should be part of the pagination.
  • elemsPerPage {Number} Number of elements per page.

Returns:

  • {Object} The created instance.

Instance API

Each basicPaginate instance has a handful of handy functions. Below are all of them along with a short description.

.pages()

Returns an array in which each item contains the DOM element/node objects of a page.

Example:

const pages = instance.pages()

Returns:

  • {Array} Array in which each item contains the DOM element/node objects of a page.

.length()

Returns the total number of pages.

Example:

const length = instance.length()

Returns:

  • {Number} Total number of pages.

.current()

Returns the current page index.

Example:

const current = instance.current()

Returns:

  • {Number} Current page index.

.goto(newIndex)

Navigates to a specific page.

Example:

instance.goto(0)

Parameters:

  • newIndex {Number} Index of the page to be displayed.

.first()

Navigates to the first page.

Example:

instance.first()

.last()

Navigates to the last page.

Example:

instance.last()

.prev()

Navigates to the previous page or to the last page when the current page is already the first one.

Example:

instance.prev()

.next()

Navigates to the next page or to the first page when the current page is already the last one.

Example:

instance.next()

.render(renderer)

basicPaginate doesn't render anything by default. Use this function to build the HTML of your pagination and to render it onto your site.

The render function accepts a function that will called every time the page of the pagination changes. It receives the current instance and allows you to generate the HTML for the pagination. Return the created element or the element containing the generated element and basicPaginate will look for special data attributes to automatically bind events. The supported attributes are data-basicpaginate-first, data-basicpaginate-last, data-basicpaginate-prev, data-basicpaginate-next and data-basicpaginate-goto. Their behaviour is equal to the functions of the instance. You can also skip the attributes or return nothing to handle the event binding on your own.

Examples:

instance.render((instance) => {

	const placeholder = document.querySelector('.placeholder')

	// Use the data attributes of basicPaginate to get automatic event binding
	placeholder.innerHTML = `
		<div class="pagination">
			<button data-basicpaginate-first>First</button>
			<button data-basicpaginate-prev>←</button>
			${ instance.pages().map((_, index) => `
				<button class="${ index === instance.current() ? 'active' : '' }" data-basicpaginate-goto="${ index }">${ index + 1 }</button>
			`).join('') }
			<button data-basicpaginate-next>→</button>
			<button data-basicpaginate-last>Last</button>
		</div>
	`

	// Return the created element so basicPaginate can look for special attributes
	return placeholder

})
instance.render((instance) => {

	const placeholder = document.querySelector('.placeholder')

	placeholder.innerHTML = `
		<div class="pagination">
			<button class="pagination__prev">←</button>
			<button class="pagination__next">→</button>
		</div>
	`

	// Handle the event binding on your own
	placeholder.querySelector('.pagination__prev').onclick = instance.prev
	placeholder.querySelector('.pagination__next').onclick = instance.next

})
instance.render((instance) => {

	const placeholder = document.querySelector('.placeholder')

	// Build the HTML of your pagination with document.createElement
	const prev = document.createElement('button')
	const next = document.createElement('button')

	prev.innerText = '←'
	next.innerText = '→'

	// Handle the event binding on your own
	prev.onclick = instance.prev
	next.onclick = instance.next

})