Socket
Socket
Sign inDemoInstall

react-data-grid

Package Overview
Dependencies
Maintainers
1
Versions
1047
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-data-grid

Feature-rich and customizable data grid React component


Version published
Maintainers
1
Created

What is react-data-grid?

react-data-grid is a powerful and feature-rich data grid component for React applications. It provides a wide range of functionalities for displaying, editing, and managing data in a tabular format. It is highly customizable and supports various features such as sorting, filtering, grouping, and more.

What are react-data-grid's main functionalities?

Basic Grid

This code demonstrates a basic data grid with three columns: ID, Title, and Count. It displays two rows of data.

import React from 'react';
import ReactDataGrid from 'react-data-grid';

const columns = [
  { key: 'id', name: 'ID' },
  { key: 'title', name: 'Title' },
  { key: 'count', name: 'Count' }
];

const rows = [
  { id: 0, title: 'Example', count: 20 },
  { id: 1, title: 'Demo', count: 40 }
];

function BasicGrid() {
  return <ReactDataGrid columns={columns} rows={rows} />;
}

export default BasicGrid;

Editable Grid

This code demonstrates an editable data grid where users can edit the values in the cells. The changes are reflected in the grid's state.

import React from 'react';
import ReactDataGrid from 'react-data-grid';

const columns = [
  { key: 'id', name: 'ID', editable: true },
  { key: 'title', name: 'Title', editable: true },
  { key: 'count', name: 'Count', editable: true }
];

const rows = [
  { id: 0, title: 'Example', count: 20 },
  { id: 1, title: 'Demo', count: 40 }
];

function EditableGrid() {
  const [gridRows, setGridRows] = React.useState(rows);

  const onGridRowsUpdated = ({ fromRow, toRow, updated }) => {
    const updatedRows = gridRows.slice();
    for (let i = fromRow; i <= toRow; i++) {
      updatedRows[i] = { ...updatedRows[i], ...updated };
    }
    setGridRows(updatedRows);
  };

  return (
    <ReactDataGrid
      columns={columns}
      rows={gridRows}
      onRowsUpdate={onGridRowsUpdated}
    />
  );
}

export default EditableGrid;

Sortable Grid

This code demonstrates a sortable data grid where users can sort the rows by clicking on the column headers. The sorting direction can be ascending or descending.

import React from 'react';
import ReactDataGrid from 'react-data-grid';

const columns = [
  { key: 'id', name: 'ID', sortable: true },
  { key: 'title', name: 'Title', sortable: true },
  { key: 'count', name: 'Count', sortable: true }
];

const rows = [
  { id: 0, title: 'Example', count: 20 },
  { id: 1, title: 'Demo', count: 40 }
];

function SortableGrid() {
  const [gridRows, setGridRows] = React.useState(rows);
  const [sortColumn, setSortColumn] = React.useState(null);
  const [sortDirection, setSortDirection] = React.useState(null);

  const onGridSort = (column, direction) => {
    const sortedRows = [...gridRows].sort((a, b) => {
      if (direction === 'ASC') {
        return a[column] > b[column] ? 1 : -1;
      } else if (direction === 'DESC') {
        return a[column] < b[column] ? 1 : -1;
      } else {
        return 0;
      }
    });
    setGridRows(sortedRows);
    setSortColumn(column);
    setSortDirection(direction);
  };

  return (
    <ReactDataGrid
      columns={columns}
      rows={gridRows}
      onGridSort={onGridSort}
      sortColumn={sortColumn}
      sortDirection={sortDirection}
    />
  );
}

export default SortableGrid;

Other packages similar to react-data-grid

Keywords

FAQs

Package last updated on 08 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc