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

pure-react-datatable

v0.5.0

Published

A somewhat API-compatible replacement for the fantastic [DataTables](https://datatables.net/) library, written in pure React (no jQuery) so that you can use JSX and click handlers in your data cells.

Downloads

19

Readme

pure-react-datatable

A somewhat API-compatible replacement for the fantastic DataTables library, written in pure React (no jQuery) so that you can use JSX and click handlers in your data cells.

Installation

yarn add pure-react-datatable
npm i pure-react-datatable

Example

The entire repo is actually an example. We use rollup to extract just the <DataTable> component and its dependencies for publishing. See src/components/App.js for a good starting point.

You can clone the repo and run GNU make to install all the dependencies and start a webpack development server.

A basic example that looks almost identical to the one on datatables.net:

import DataTable from 'pure-react-datatable';

const jobsTable = {
    data: JOBS,
    columns: [
        { title: "Name" },
        { title: "Position" },
        { title: "Office" },
        { title: "Extn." },
        { title: "Start date" },
        { title: "Salary" }
    ],
    searchDelay: 16,
};

function App() {

    return (
        <ErrorBoundary>
            <h1>Example</h1>
            <DataTable theme={bridge} {...jobsTable} />
        </ErrorBoundary>
    )
}

Where bridge is either a CSS file compiled with CSS modules, or a mapping from pure-react-datatable class names to datatables.net class names (if you want to re-use existing styles you already have:

{
    wrapper: "dataTables_wrapper",
    table: ["dataTable","display","no-footer"],
    odd: "odd",
    even: "even",
    empty: "dataTables_empty",
    pagination: ["dataTables_paginate",theme.screenOnly],
    button: "paginate_button",
    pageInfo: "dataTables_info",
    search: ["dataTables_filter",theme.screenOnly],
    length: ["dataTables_length",theme.screenOnly],
    current: "current",
    disabled: "disabled",
    searchText: theme.printOnly,
    pageXofY: theme.printOnly,
    sortAsc: 'sorting_asc',
    sortDesc: 'sorting_desc',
    orderable: 'sorting',
    sortIcon: css.displayNone,
    sort1: 'sorting_1',
    sort2: 'sorting_2',
    sort3: 'sorting_3',
    sort4: 'sorting_3', // datatables.net stops at 3
    sort5: 'sorting_3',
    sort6: 'sorting_3',
    processing: 'dataTables_processing',
    pageNumberSpacer: 'ellipsis',
}

Where screenOnly and printOnly are simply:

@media print {
    .screenOnly {
        display: none;
    }
}

@media screen {
    .printOnly {
        display: none;
    }
}

We use these to provide a better printing experience; there's no sense printing a drop-down length menu on paper.

Styling

pure-react-datatable provides no styling so that you can customize it to match your app. Don't worry, it's super easy!

If you're using CSS modules (which I highly recommend), it's as easy as:

<DataTable theme={require('yourtheme.css')} />

If not, you'll have to create a mapping between the CSS class names that DataTable expects and your global CSS class names, like so:

const theme = {
    table: 'datatable_table',
    tr: 'datatable_tr',
}

If you use styled-components or similar CSS-in-JS, you're hooped. You can use the undocumented cellComponent and rowComponent props to provide your custom styled components, but <DataTable> isn't fully customizable yet.

The defaults for those are simple:

function DataTableRow({attrs}) {
    return <tr {...attrs}/>;
}

function DataTableCell({attrs}) {
    return <td {...attrs}/>;
}

So they should be easy to override.

Usage/integration

I recommend extending pure-react-datatable with some defaults so that you don't have to keep repeating props throughout your app:

import React from 'react';
import PureDataTable from 'pure-react-datatable';
import bridge from './bridge';
import {defaultsDeep} from 'lodash';

export default function MyDataTable({...props}) {
    defaultsDeep(props, {
        theme: bridge,
        lengthMenu: null,
        length: 25,
    });
    return <PureDataTable {...props} />;
}

Data

Client-side data is not fully implemented. However, you can implement it in userland if you feel inclined (if you do, please share). The data prop is a function that feeds you all the information you need to do searching and sorting yourself:

async data({draw,start,length,search,order,columns}) {
    return {
        draw,
        recordsTotal: CLIENTS.length,
        recordsFiltered: CLIENTS.length,
        data: CLIENTS.slice(start,start+length),
    }
}

It expects you to return a Promise, so you can either do this client-side or send the arguments to your server and do it there. BYO ajax library.

If you simply forward these arguments to your server, it should work with any existing endpoints you have, if you previously used datatables.net -- as long as you are using the 1.10+ API (hungarian notation not supported).

API

To get access to the API methods from a parent component use the api prop to bind a local copy of the api methods to an object in the parent component. This is handy for custom external filtering.

API Methods:

Example
class MyComponent extends React.Component {
    refreshMyPureDataTable() {
        this.myDtApi.draw();
    }
    
    render() {
        return <PureDataTable api={api => this.myDtApi = api}/>;
    }
}

License

MIT.