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

react-mui-dynamic-data-table

v1.0.5

Published

A dynamic data table component for React that uses Material UI

Downloads

38

Readme

react-mui-dynamic-data-table

npm version npm npm

A dynamic data table component for React that uses Material UI.

Features

  • Sortable columns
  • Pagination
  • Searching
  • Filtering via modal, defined in column definitions
    • Checkbox inputs (default)
    • Range inputs for numbers and dates
    • Dropdown inputs
  • Row selection (single + multi select)
  • Row actions and events (click, edit, and open buttons)
  • Custom rendering, searching, and sorting functions
    • Allows user defined callbacks for rendering, searching, and sorting table entries
    • Supports JSON-like objects and React.ReactNode

Installation

You can install this package with either npm or yarn using either of the following commands

npm install react-mui-dynamic-data-table
yarn add react-mui-dynamic-data-table

Usage

Basic usage

import { MuiDynamicDataTable } from 'react-mui-dynamic-data-table';

const data = [
  { firstName: 'Bob', lastName: 'Smith', age: 24 },
  { firstName: 'Alice', lastName: 'Thompson', age: 26 },
];

export function App() {
    return (
        <MuiDynamicDataTable
            columns={[
                {
                    name: 'firstName',
                    title: 'First name',
                },
                {
                    name: 'lastName',
                    title: 'Last name',
                },
                {
                    name: 'age',
                    title: 'Age',
                },
            ]}
            data={data}
    )>
}

Searching

Enable sorting within options to sort columns.

    options={{
        search: {
            enabled: true,
            placeholder: 'Search items...'
        }
    }}

Column sorting

Enable searching within options to search within the the table.

    options={{
        sort: {
            enabled: true,
            by: 'firstName'
        }
    }}

Filtering

Enable filtering within options to allow filtering, and set canFilter on each column definition to enable that column to be filtered.

Set the style property of the column definition to control to type of component rendered in the filter modal for the column.

    columns={[
        {
            name: 'firstName',
            title: 'First name',
            style: 'default', // Checkbox component
            canFilter: true
        },
        {
            name: 'lastName',
            title: 'Last name',
            style: 'select', // Select/dropdown component
            canFilter: true
        },
        {
            name: 'age',
            title: 'Age',
            style: 'number', // Number range component
            canFilter: true
        },
    ]}
    options={{
        filter: {
            enabled: true,
        }
    }}

Custom rendering

You can render an entry as a React Node using getRenderedEntry in the column definitions. This should be used in conjunction with getSearchEntry when searching is enabled.

Note getRenderedEntry and getSearchEntry are especially useful for rendering JSON-like objects. See the code sandbox for a more complex example. If rendering a JSON-like object, it is recommended to use getFilterIdentifier, which acts like React's key prop to uniquely identify a table entry.

    columns={[
        {
            name: 'firstName',
            title: 'First name',
            style: 'select',
            getSearchEntry: (firstName) => firstName,
            getRenderedEntry: (firstName) => <span style={{color: 'red'}}>{firstName}</span> // Text is red on table and filter modal
            canFilter: true,
        },
    ]}

Row events

You can customize what happens when a row is clicked by defining a callback for onRowClick.

const handleRowClick = (row) => {
    console.log(row);
}
return (
    <MuiDynamicDataTable
        ...
        onRowClick={handleRowClick}
    >
    )

Row actions

You can choose to show action buttons in the table for editing and opening a row.

const handleEdit = (row) => {
    console.log(row);
}
const handleOpen = (row) => {
    console.log(row);
}
return (
    <MuiDynamicDataTable
        ...
        options={{
            edit: {
                show: true,
                onClick: handleEdit,
            },
            open: {
                show: true,
                onClick: handleOpen,
            }
        }}
    >
    )

Asynchronous data

If your data is fetched asynchronously, you can choose to show a loading indicator while the data is being fetched.

type DataType = {firstName: string, lastName: string, age: number};
const [loading, setLoading] = useState();
const [data, setData] = useState<DataType[]>();

useEffect(() => {
    async function getData() {
        setLoading(true);
        const response = await fetchData();
        setData(response);
        setLoading(false);
    }
    getData();
}, [])

return (
    <MuiDynamicDataTable
        columns={columns}
        data={data}
        loading={loading}
    >
    )

Code sample and live demo

Check out the code sandbox for a live demo, or clone the example application under /example.