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

axpager

v1.2.4

Published

a simple paginator support ajax request and array paging.

Downloads

9

Readme

ajax-paginator

A useful paginator supports ajax request and static array data paging.

Installation

ES2015 via npm

npm install axpager

Web via script link

<script src="dist/axpager.umd.js"></script>

Themes

<link rel="stylesheet" href="dist/themes/axpager.theme.light.css">

Usage

ES2015

import {Paginator} from 'axpager';

UMD

const {Paginator} = axpager;

Example

Rewrite function PageConfig#getPagedResource to adapt ajax request paging if your response data format cannot match {data: any[], length: number} :

// Bulit-in default implementation.
getPagedResource: response => ({data: response.data, length: response.pager.recordCount})
const paginator = Paginator.init(document.getElementById('pager'), {
  getPagedResource: // adapt response data for ajax
});
// paginator.ajax(url, requestOption)
// paginator.resource(array, requestOption)
paginator.of(url | array, {
    success: (list, event, req) => {
        // ...
    }
});

Ajax request paging

const paginator = Paginator.init(document.getElementById('pager'));
paginator.of('http://localhost:8080/users', {
  data:{
    username: 'jack'
  },
  before: init => {
    // send cookie to server
    // default xhr: init.withCredentials = true;
    // fetch api: init.credentials = 'include'
  },
  success: (list, event, req) => {
    // GET: actually request url: 'http://localhost:8080/users?page=1&size=10&username=jack'
  },
  error: err => {
    
  }
});

Array data paging

const paginator = Paginator.init(document.getElementById('pager'));
paginator.of([1,2,3,4,5,6,7,8,9], {
  data: {
    num: 5
  },
  success: (list, event, req) => {
    // list: [5]
    // req: {num: 5}
  },
  filter: (item, req) => {
    // item: each num of array.
    // req: {num: 5}
    return item === req.num;
  }
});

Above examples both are default PageConfig and RequestOption.

Get into dist/example/index.html demo preview.

Constructor

Paginator(container: HTMLElement, config?: PageConfig)

Static method: init(container: HTMLElement, config?: PageConfig)

Instance properties

  • pages readonly

    Total pages.

  • pageParams readonly

    Ajax paging request page params.

  • pageEvent readonly

    Paging event data.

Instance methods

  • ajax(url: string, option?: RequestOption)

    Ajax request paging.

  • resource(data: any[], option?: RequestOption)

    Static array data paging.

  • of(target: string | any[], option?: RequestOption)

    Request paging, ajax or resource will be called depends on target type.

  • refresh()

    Refresh current page's data.

  • disable(isDisable: boolean)

    If true disable all actions (select, buttons, actions events and paging action).

  • goto(page: number)

    Goto target page number.

Configuration

PageConfig

Properties

  • itemsPerPageLabel optional

    Items per page lebel text, default 每页条数 .

  • hidePageSize optional

    Hide page size panel( items per page babel and page size options), default false .

  • showPageSizeOptions optional

    Show page size options, default true .

  • showFirstLastButtons optional

    Show first and last button, default true .

  • initPageNumber optional

    Instance init page number, default 1 .

  • initPageSize optional

  • Instance init page size, default pageSizeOptions[0] or 10 .

  • pageSizeOptions optional

    Page size options, default [10, 15, 30] .

  • pageRadius optional

    Half of page numbers length, default 0 .

    // current page: 5
    // 2
    [3, 4, 5, 6]
    // 3
    [2, 3, 4, 5, 6, 7]
  • pageNumbersType optional

    Page numbers element type, default button , support select , working when pageRadius > 1 .

    // pageRadius: 2
      
    // select example:
    // |< < [ ] > >|
    //      [3]
    //      [4]
    //      [5]
    //      [6]
      
    // button example:
    // |< < (3) (4) (5) (6) > >|
  • firstPageLabel optional

    First page button title text, default 第一页 .

  • previousPageLabel optional

    Previous page button title text, default 上一页 .

  • nextPageLabel optional

    Next page button title text, default 下一页 .

  • lastPageLabel optional

    Last page button title text, default 最后一页 .

Methods

  • ajaxAdapter optional ajax

    Ajax request adapter, custom ajax request implementation, default:

    () => new XMLHttpRequestAdapter()
  • getRangeLabel optional

    Get range label text, default:

    (page: number, size: number, pages: number, length: number) => `第${page}/${pages}页,共${length}条`
  • getPageParams optional ajax

    Get necessary ajax request page params, default:

    (page: number, size: number) => ({page: page, size: size})
  • getPagedResource optional ajax

    Get custom paged resource format from ajax request response, default:

    (response: any) => ({data: response.data, length: response.pager.recordCount})
  • changes optional

    Before paging action request changes callback, default:

    (pageEvent, eventTarget) => void (0)

RequestOption

Properties

  • method optional ajax init

    AJAX request method, default GET, support POST .

  • data optional init

    Request params such as query parameters from form, default {}, support {} and FormData, data will be parsed:

    • GET: {} -> form-urlencode

    • POST:

      {} -> form-urlencode (default)

      {} -> json (Content-Type : application/json)

      {} -> FormData (Content-Type : multipart/form-data)

      FormData -> FormData

  • headers optional ajax init

    Http headers.

  • timeout optional ajax init

    Ajax request timeout, default -1 .

Methods

  • before optional init

    Before request paging callback, default:

    (init: XMLHttpRequest | RequestInit | any) => void (0)
  • success

    Paging request success callback, default:

    (data: any[], pageEvent: PageEvent, requestData: {} | FormData | any) => void(0)
  • error optional ajax

    Paging request error callback, default:

    (error: any) => void(0)
  • finish optional

    Paging request finished callback, default:

    () => void(0)
  • filter optional resource

    Static array data paging filter, such as Array#filter , default:

    (item: any, requestData: {} | FormData | any) => true

AjaxAdapter

Basic ajax request adapter, built-in:

  • XMLHttpRequestAdapter default

    Base on XMLHttpRequest default implementation.

  • FetchAdapter

    Based on fetch api adapter.

AjaxAdapter

Methods
  • request

    (url: string, pageParams: {}, reqOption: RequestInitOption): Promise<any>

    Request method, resolve response and reject exception.

Example
class MyAdapter implements AjaxAdapter {
	request(url: string, pageParams: {}, reqOption: RequestInitOption): Promise<any> {
      return new Promise((resolve, reject) => {
        fetch(url, option)
                .then(response => {
                  if (response.ok) {
                    response.json().then(resolve);
                    return;
                  }
                  reject(response.status + ': ' + (response.statusText || 'request failed.'));
                }).catch(reject);
      });
	}
}