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

paginated-table-gl

v2.0.2

Published

This mini-project was born simply because I found myself rewriting this functionality. It provides simple pagination, filtering and sorting of tabulated data. It is very simple to use; please see the example below. Feel free to use, copy, change as you se

Downloads

5

Readme

Paginated Tables for React

This mini-project was born simply because I found myself rewriting this functionality. It provides simple pagination, filtering and sorting of tabulated data. It is very simple to use; please see the example below. Feel free to use, copy, change as you see fit.

Installation

npm install paginated-table-gl

Changes compared to version 1.x

This is version 2.x, which is not compatible with 1.x versions. The main change is that the columns prop now contains all the metadata required to manage how the data is rendered. This allows greater flexibility, particularly in customising the look and feel of the table. Each data element can be styled individually. It is also possible to pass JSX fragments to the table in order to render more complex objects.

The columns prop now takes an array of objects:

const columns = [
    {
        name: 'first_name',
        title: 'First Name',
        sortable: true,
        filterable: true,
        header:
            {
                style: {width: '30%'},
                className: 'myColumn'
            },
        body:
            {
                style: {backgroundColor: 'pink'},
                className: 'myCell'
            }
    },
    ...
];

Example Usage

import { PaginatedTable } from 'paginated-table-gl';

const TestPage = (props) => {

    // this would normally come from a database,
    // we'll use this for testing...
    const data = [
        {
            name: "Mr. Test",
            description: "This is Mr. Test.",
            isActive: true
        },
        {
            name: "Mrs. Test",
            description: "This is Mrs Test.",
            isActive: true
        },
        {
            name: "Big Dave",
            description: "This is big Dave",
            isActive: false
        }
    ];

    // this is what we do when the "View" button is clicked
    // in the table (see _actions property in tableData)
    const doView = (person) => {
        console.log(person);
    }

    // this function is called when the checkbox is changed
    const toggleActive = (person) => {
      person.isActive = !person.isActive;
    }

    // we prepare the data so it is in a format suitable
    // for reading. the _actions property accepts an array
    // of objects with name and onClick properties.

    // we can also pass JSX fragments to render more complex objects
    const tableData = data.map(d => ({
        name: d.name.toUpperCase(),
        description: d.description,
        isActive: <input type="checkbox" name={d.name} checked={d.isActive} onChange={() => toggleActive(d)} />,
        _actions: [
            {
                name: 'view',
                onClick: () => doView(d)
            }
        ]
    }));

    // we prepare the rest of the props the PaginatedTable needs
    const columns = [
        {
            name: 'name',
            title: 'Name',
            sortable: true,
            filterable: true,
            headers: {
                style: {backgroundColor: 'pink'}
            }
        },
        {
            name: 'description',
            title: 'Description',
            body: {
                className: 'descriptionCell'
            }
        },
        {
            name: 'isActive',
            title: 'Active'
        },
        {
            name: '_actions',
            title: 'Actions'
        }
    ]

    return (
        <>
            <h1>Paginated Table Example</h1>
            <PaginatedTable
                data={tableData}
                columns={columns}
                recordsPerPage={10}
            />
        </>
    )
}

export default TestPage;

Props

data

An array of objects to be displayed in the table. The table will paginate them according to the recordsPerPage prop (see later). Note that the _actions property is a special property which is interpreted by the module, and used to create actions. It is an array of objects with properties name and onClick. name is the label which is displayed on the button, onClick is the function that is executed when the button is clicked. You can pass the entire record to the function if necessary. Each object in the _actions array will create a button in the same table cell.

columns

An array of objects containing metadata to describe the column.

const columns = [
    {
        name: 'first_name',
        title: 'First Name,
        sortable: true,
        filterable: true,
        header:
            {
                style: {width: "30%"},
                className: "myColumn"
            },
        body:
            {
                style: {backgroundColor: pink},
                className: "myCell"
            }
    },
    ...
];

All properties are optional apart from name which must correspond to a property on the object being displayed. In the absence of a title, the name will be used as the column title. if title is an empty string, the column will have no title.

recordsPerPage

A positive integer which sets how many records are show on each page of the table.

baseClass

A css class which will be assigned to the <table> HTML element. The class substitutes the default paginated-table.

noDataMessage

The message to be displayed when no data is present in the table. defaults to "No Data Found".

currentPage

Allows manually setting the page number to display in the table (useful if you need someone to navigate directly to a particular page of the table, for saved searches etc.)