What is ag-grid-react?
ag-grid-react 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 large datasets, making it suitable for enterprise-level applications.
What are ag-grid-react's main functionalities?
Basic Grid Setup
This code sets up a basic grid with three columns: Make, Model, and Price. It also provides some sample row data to display in the grid.
import React from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
const App = () => {
const columnDefs = [
{ headerName: 'Make', field: 'make' },
{ headerName: 'Model', field: 'model' },
{ headerName: 'Price', field: 'price' }
];
const rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxster', price: 72000 }
];
return (
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
/>
</div>
);
};
export default App;
Sorting and Filtering
This code demonstrates how to enable sorting and filtering on the columns of the grid. Users can click on the column headers to sort and use the filter input to filter the data.
import React from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
const App = () => {
const columnDefs = [
{ headerName: 'Make', field: 'make', sortable: true, filter: true },
{ headerName: 'Model', field: 'model', sortable: true, filter: true },
{ headerName: 'Price', field: 'price', sortable: true, filter: true }
];
const rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxster', price: 72000 }
];
return (
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
/>
</div>
);
};
export default App;
Row Selection
This code demonstrates how to enable row selection in the grid. It also includes an event handler to log the selected rows to the console.
import React, { useState } from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
const App = () => {
const [rowData] = useState([
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxster', price: 72000 }
]);
const [columnDefs] = useState([
{ headerName: 'Make', field: 'make' },
{ headerName: 'Model', field: 'model' },
{ headerName: 'Price', field: 'price' }
]);
const onSelectionChanged = (event) => {
const selectedRows = event.api.getSelectedRows();
console.log('Selected rows:', selectedRows);
};
return (
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
rowSelection="single"
onSelectionChanged={onSelectionChanged}
/>
</div>
);
};
export default App;
Custom Cell Renderer
This code demonstrates how to use a custom cell renderer to change the appearance of cell values based on their content. In this example, the price values are colored red if they are greater than 50000 and green otherwise.
import React from 'react';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/ag-grid.css';
import 'ag-grid-community/styles/ag-theme-alpine.css';
const CustomCellRenderer = (props) => {
return (
<span style={{ color: props.value > 50000 ? 'red' : 'green' }}>
{props.value}
</span>
);
};
const App = () => {
const columnDefs = [
{ headerName: 'Make', field: 'make' },
{ headerName: 'Model', field: 'model' },
{ headerName: 'Price', field: 'price', cellRenderer: 'customCellRenderer' }
];
const rowData = [
{ make: 'Toyota', model: 'Celica', price: 35000 },
{ make: 'Ford', model: 'Mondeo', price: 32000 },
{ make: 'Porsche', model: 'Boxster', price: 72000 }
];
return (
<div className="ag-theme-alpine" style={{ height: 400, width: 600 }}>
<AgGridReact
columnDefs={columnDefs}
rowData={rowData}
frameworkComponents={{ customCellRenderer: CustomCellRenderer }}
/>
</div>
);
};
export default App;
Other packages similar to ag-grid-react
react-table
react-table is a lightweight, fast, and extendable data grid built for React. It focuses on providing a simple and flexible API for building tables with features like sorting, filtering, and pagination. Compared to ag-grid-react, react-table is more lightweight and easier to customize but may lack some of the advanced features and performance optimizations of ag-grid-react.
material-table
material-table is a data table component built on top of Material-UI. It provides a rich set of features like sorting, filtering, grouping, and editing, along with a modern and responsive design. Compared to ag-grid-react, material-table offers a more integrated experience with Material-UI and is suitable for applications already using Material-UI for their UI components.
react-data-grid
react-data-grid is a highly customizable and performant data grid component for React. It offers features like sorting, filtering, grouping, and cell editing. Compared to ag-grid-react, react-data-grid is more focused on performance and customization, making it a good choice for applications that require a high degree of control over the grid's behavior and appearance.
AG Grid React Component
AG Grid is a fully-featured and highly customizable JavaScript data grid.
It delivers outstanding performance, has no 3rd party dependencies and integrates smoothly with React as React Component. Here's how our grid looks like with multiple filters and grouping enabled:
Features
Besides the standard set of features you'd expect from any grid:
- Column Interactions (resize, reorder, and pin columns)
- Pagination
- Sorting
- Row Selection
Here are some of the features that make AG Grid stand out:
- Grouping / Aggregation *
- Accessibility support
- Custom Filtering
- In-place Cell Editing
- Records Lazy Loading *
- Server-Side Records Operations *
- Live Stream Updates
- Hierarchical Data Support & Tree View *
- Customizable Appearance
- Customizable Cell Contents
- State Persistence
- Keyboard Navigation
- Data Export to CSV
- Data Export to Excel *
- Excel-like Pivoting *
- Row Reordering
- Copy / Paste
- Column Spanning
- Pinned Rows
- Full Width Rows
- Integrated Charting
- Sparklines
* The features marked with an asterisk are available in the enterprise version only.
Check out developers documentation for a complete list of features or visit our official docs for tutorials and feature demos.
Usage Overview
Use the setup instructions below or go through a 5-minute-quickstart guide.
Install dependencies
$ npm i --save ag-grid-community ag-grid-react
Import the grid and styles
import {AgGridReact} from 'ag-grid-react';
import 'ag-grid-community/styles//ag-grid.css';
import 'ag-grid-community/styles//ag-theme-quartz.css';
Set the grid's configuration in a parent component
class App extends Component {
constructor(props) {
super(props);
this.state = {
columnDefs: [
{headerName: "Make", field: "make"},
{headerName: "Model", field: "model"},
{headerName: "Price", field: "price"}
],
rowData: [
{make: "Toyota", model: "Celica", price: 35000},
{make: "Ford", model: "Mondeo", price: 32000},
{make: "Porsche", model: "Boxster", price: 72000}
]
}
}
...
}
Render the grid as the AgGridReact
child component
class App extends Component {
constructor(props) {...}
render() {
return (
<div
className="ag-theme-quartz"
style={{
height: '500px',
width: '600px'
}}
>
<AgGridReact
columnDefs={this.state.columnDefs}
rowData={this.state.rowData}>
</AgGridReact>
</div>
);
}
}
Issue Reporting
If you have found a bug, please report them at this repository issues
section. If you're using Enterprise version please use the private ticketing system to do that.
Asking Questions
Look for similar problems on StackOverflow using the ag-grid
tag. If nothing seems related, post a new message there. Do not use GitHub issues to ask questions.
Contributing
AG Grid is developed by a team of co-located developers in London. If you want to join the team check out our jobs listing or send your application to info@ag-grid.com.
License
This project is licensed under the MIT license. See the LICENSE file for more info.