2.0.7 • Published 6 months ago

rsl-pagination v2.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Rs-Pagination

A simple powerful pagination, can be handled billions data.

animation.gif

img.png

Screenshot from 2023-11-10 19-48-31.png

NPM

Installation

Install rsl-pagination with npm:

npm install rsl-pagination

Usage

Very easy to use. Just provide props

import {useEffect, useState} from "react";

// import this
import RsPagination from 'rsl-pagination';
import "rsl-pagination/dist/index.css"
import data from "./data.json"

function fetchDataWithPagination(page, pageSize) {
    return {
        items: data.slice(pageSize * (page - 1), pageSize * page),
        totalItem: data.length,
    }
}

export default function Index() {
    const [items, setItems] = useState({
        items: [],
        totalItem: 0
    })

    const [state, setState] = useState({
        pageSize: 10,
        page: 1
    });

    function handlePageChange({currentPage}) {
        setState(prevState => ({
            ...prevState,
            page: currentPage
        }))
    }

    useEffect(() => {
        fetchPosts(state.page, state.pageSize)
    }, [state.page]);

    function fetchPosts(page, pageSize) {
        const response = fetchDataWithPagination(page, pageSize)
        setItems(prev => ({
            ...prev,
            ...response
        }))
    }

    return (
        <>
            <div>
                <h1>Rs Pagination</h1>
                <table>
                    <tr>
                        <th>User ID</th>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Body</th>
                    </tr>
                    {items?.items?.map(item => (
                        <tr key={item.id}>
                            <td>{item.id}</td>
                            <td>{item.userId}</td>
                            <td>{item.title}</td>
                            <td>{item.body.substring(0, 50)}
                            </td>
                        </tr>
                    ))}
                </table>
                <br/>
                <RsPagination
                    className="pagination"
                    currentPage={state.page}
                    onChange={handlePageChange}
                    buttonSize={20}
                    perPageRow={state.pageSize}
                    totalItem={items.totalItem ?? 0}
                />
            </div>
        </>
    );
}

JSON Data file

data.json

Check Example Live example

Params

NameTypeDefaultDescription
classNameString""Optional. apply class in root element
currentPagenumberfalseRequired. current page number
pageSizenumber5Optional.
totalItemsnumber0Required. Total items of list
onChangeonChange: (arg: { currentPage: number, perPageRow: number }) => voidRequired.
buttonSizenumber10Optional. How many page button show
2.0.7

6 months ago

2.0.6

6 months ago

2.0.5

6 months ago

2.0.4

6 months ago

2.0.3

6 months ago

2.0.2

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago