How to implement AG Grid in React
Today we’ll show you how to implement AG Grid in React. In this article, we will create a sample example to load static data in a grid.
In the previous articles, we had created several examples on DataTable in React.
- Implement DataTable in React
- Datatable with pagination in React
- Server side pagination with React DataTable
Demo Application
Steps to implement AG Grid in React
1. Create a react app and install npm dependencies
Let’s create a react app using the create-react-app
. Run the following command to create an application.
1 | npx create-react-app react-ag-grid-example |
Here, we are loading the data in table using the React AG Grid npm package. Run the following command to install the react AG Grid.
1 | npm install --save ag-grid-community ag-grid-react |
2. Create component to load data in grid
Let’s import the AgGridColumn
and AgGridReact
from the ag-grid-react
package. We have to import the ag-grid CSS along with the package.
Here, we have used static data to load into the grid and also used the cellClass
& cellRendererFramework
to render the image using avatar
field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import { AgGridColumn, AgGridReact } from 'ag-grid-react'; import 'ag-grid-community/dist/styles/ag-grid.css'; import 'ag-grid-community/dist/styles/ag-theme-alpine.css'; import './App.css'; const rowData = [ { id: 1, first_name: "George", last_name: "Bluth", avatar: "https://reqres.in/img/faces/1-image.jpg" }, { id: 2, first_name: "Janet", last_name: "Weaver", avatar: "https://reqres.in/img/faces/2-image.jpg" }, { id: 3, first_name: "Emma", last_name: "Wong", avatar: "https://reqres.in/img/faces/3-image.jpg" }, { id: 4, first_name: "Eve", last_name: "Holt", avatar: "https://reqres.in/img/faces/4-image.jpg" }, { id: 5, first_name: "Charles", last_name: "Morris", avatar: "https://reqres.in/img/faces/5-image.jpg" }, { id: 6, first_name: "Tracey", last_name: "Ramos", avatar: "https://reqres.in/img/faces/6-image.jpg" }, { id: 7, first_name: "Michael", last_name: "Lawson", avatar: "https://reqres.in/img/faces/7-image.jpg" }, { id: 8, first_name: "Lindsay", last_name: "Ferguson", avatar: "https://reqres.in/img/faces/8-image.jpg" }, { id: 9, first_name: "Tobias", last_name: "Funke", avatar: "https://reqres.in/img/faces/9-image.jpg" }, { id: 10, first_name: "Byron", last_name: "Fields", avatar: "https://reqres.in/img/faces/10-image.jpg" }, { id: 11, first_name: "George", last_name: "Edwards", avatar: "https://reqres.in/img/faces/11-image.jpg" }, { id: 12, first_name: "Rachel", last_name: "Howell", avatar: "https://reqres.in/img/faces/12-image.jpg" } ]; function App() { const avatarFormatter = ({ value }) => { return <img src={value} width="50px" height="50px" /> } return ( <div className="App"> <h2>Implement AG Grid in React - <a href="https://www.cluemediator.com" target="_blank" rel="noopener">Clue Mediator</a></h2> <div className="ag-theme-alpine ag-style"> <AgGridReact defaultColDef={{ flex: 1 }} rowHeight={60} rowData={rowData} > <AgGridColumn field="first_name" headerName="First Name" sortable={true} filter={true} cellClass="vertical-middle" /> <AgGridColumn field="last_name" headerName="Last Name" sortable={true} filter={true} cellClass="vertical-middle" /> <AgGridColumn field="email" headerName="Email" sortable={true} filter={true} cellClass="vertical-middle" /> <AgGridColumn field="avatar" headerName="Avatar" sortable={true} filter={true} cellRendererFramework={avatarFormatter} cellClass="vertical-middle" /> </AgGridReact> </div> </div> ); } export default App; |
Let’s apply the following CSS for the grid.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | .ag-style { height: 500px; width: 100%; } .vertical-middle { display: flex; align-items: center; } .vertical-middle img { display: block; border: 1px solid #ddd; border-radius: 3px; } |
3. Output
Run the react application and check the output in the browser.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂