Introduction

In this article, we will learn how to use ag-grid in a React.js application. ag-Grid  is a component used for showing data in tabular format. By using ag-Grid, developers can easily extend functionality according to their requirements.
Prerequisites
  • Basic knowledge of React.js
  • Visual Studio Code IDE should be installed on your system.
  • Basic knowledge of Bootstrap and HTML.

Create ReactJS project

First, let's create a React application with the following command.
  1. npx create-react-app reduxparttwo
Open the newly created project in Visual Studio Code and install ag-Grid using the following command.
  1. npm install --save ag-grid-community ag-grid-react
Now install bootstrap in this project by using the following command.
  1. npm install --save bootstrap
Now, open the index.js file and add import Bootstrap.
  1. import 'bootstrap/dist/css/bootstrap.min.css';
Now go to the src folder and add a new component, named 'Aggriddemo.js'.
Add ag grid and style reference in this component, add the following lines in this component.
  1. import { AgGridReact } from 'ag-grid-react';
  2. import 'ag-grid-community/dist/styles/ag-grid.css';
  3. import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
ag-Grid has differenct themes. We can customize these themes. Add the following code in this component:
  1. import React, { Component } from 'react'
  2. import { AgGridReact } from 'ag-grid-react';
  3. import 'ag-grid-community/dist/styles/ag-grid.css';
  4. import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
  5. export class Aggriddemo extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. columnDefs: [
  10. { headerName: "Id", field: "Id", sortable: true, filter: true },
  11. { headerName: "Name", field: "Name", sortable: true, filter: true },
  12. { headerName: "Age", field: "Age", sortable: true, filter: true },
  13. { headerName: "Address", field: "Address", sortable: true, filter: true },
  14. { headerName: "City", field: "City", sortable: true, filter: true },
  15. { headerName: "Salary", field: "Salary", sortable: true, filter: true },
  16. { headerName: "Department", field: "Department", sortable: true, filter: true }
  17. ],
  18. rowData: [
  19. {
  20. Id: "100",
  21. Name: "Sanwar",
  22. Age: "25",
  23. Address: "Jaipur",
  24. City: "Jaipur",
  25. Salary: "500000",
  26. Department: "IT",
  27. },
  28. {
  29. Id: "2",
  30. Name: "Nisha",
  31. Age: "25",
  32. Address: "C-Scheme",
  33. City: "Jaipur",
  34. Salary: "500000",
  35. Department: "IT",
  36. },
  37. {
  38. Id: "3",
  39. Name: "David",
  40. Age: "25",
  41. Address: "C-Scheme",
  42. City: "Jaipur",
  43. Salary: "500000",
  44. Department: "IT",
  45. },
  46. {
  47. Id: "4",
  48. Name: "Sam",
  49. Age: "25",
  50. Address: "C-Scheme",
  51. City: "Jaipur",
  52. Salary: "500000",
  53. Department: "IT",
  54. },
  55. {
  56. Id: "5",
  57. Name: "Jyotsna",
  58. Age: "25",
  59. Address: "C-Scheme",
  60. City: "Mumbai",
  61. Salary: "500000",
  62. Department: "IT",
  63. },
  64. {
  65. Id: "6",
  66. Name: "Sid",
  67. Age: "25",
  68. Address: "C-Scheme",
  69. City: "Bangalore",
  70. Salary: "500000",
  71. Department: "IT",
  72. },
  73. {
  74. Id: "7",
  75. Name: "Chavi",
  76. Age: "25",
  77. Address: "C-Scheme",
  78. City: "Noida",
  79. Salary: "500000",
  80. Department: "IT",
  81. },
  82. {
  83. Id: "8",
  84. Name: "Nisha",
  85. Age: "25",
  86. Address: "C-Scheme",
  87. City: "Delhi",
  88. Salary: "500000",
  89. Department: "IT",
  90. }
  91. ]
  92. }
  93. }
  94. render() {
  95. return (
  96. <>
  97. <div class="row" style={{marginTop:"10px"}}>
  98. <div class="col-sm-12 btn btn-info">
  99. Add ag-Grid to ReactJS App
  100. </div>
  101. </div>
  102. <div class="row">
  103. <div class="col-md-12">
  104. <div class="card">
  105. <div class="card-body position-relative">
  106. <div className="ag-theme-alpine" style={{ height: "550px" }}>
  107. <AgGridReact
  108. columnDefs={this.state.columnDefs}
  109. rowData={this.state.rowData}
  110. >
  111. </AgGridReact>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. </>
  118. );
  119. }
  120. }
  121. export default Aggriddemo
Add reference of this component in the app.js file, and add the following code in the app.js file.
  1. import React from 'react';
  2. import logo from './logo.svg';
  3. import './App.css';
  4. import Aggriddemo from './Aggriddemo'
  5. function App() {
  6. return (
  7. <div className="App">
  8. <Aggriddemo/>
  9. </div>
  10. );
  11. }
  12. export default App;
Now, run the project by using 'npm start' command and check the result.
How To Use Ag-Grid In ReactJS
We can also add pagination, sorting and filtering in ag-grid. To add filtering shorting and pagination add the following property in ag grid.
  1. pagination="true" paginationPageSize="5" floatingFilter="true"
Add the following code in the component and check the result:
  1. import React, { Component } from 'react'
  2. import { AgGridReact } from 'ag-grid-react';
  3. import 'ag-grid-community/dist/styles/ag-grid.css';
  4. import 'ag-grid-community/dist/styles/ag-theme-alpine.css';
  5. export class Aggriddemo extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. columnDefs: [
  10. { headerName: "Id", field: "Id", sortable: true, filter: true },
  11. { headerName: "Name", field: "Name", sortable: true, filter: true },
  12. { headerName: "Age", field: "Age", sortable: true, filter: true },
  13. { headerName: "Address", field: "Address", sortable: true, filter: true },
  14. { headerName: "City", field: "City", sortable: true, filter: true },
  15. { headerName: "Salary", field: "Salary", sortable: true, filter: true },
  16. { headerName: "Department", field: "Department", sortable: true, filter: true }
  17. ],
  18. rowData: [
  19. {
  20. Id: "100",
  21. Name: "Sanwar",
  22. Age: "25",
  23. Address: "Jaipur",
  24. City: "Jaipur",
  25. Salary: "500000",
  26. Department: "IT",
  27. },
  28. {
  29. Id: "2",
  30. Name: "Nisha",
  31. Age: "25",
  32. Address: "C-Scheme",
  33. City: "Jaipur",
  34. Salary: "500000",
  35. Department: "IT",
  36. },
  37. {
  38. Id: "3",
  39. Name: "David",
  40. Age: "25",
  41. Address: "C-Scheme",
  42. City: "Jaipur",
  43. Salary: "500000",
  44. Department: "IT",
  45. },
  46. {
  47. Id: "4",
  48. Name: "Sam",
  49. Age: "25",
  50. Address: "C-Scheme",
  51. City: "Jaipur",
  52. Salary: "500000",
  53. Department: "IT",
  54. },
  55. {
  56. Id: "5",
  57. Name: "Jyotsna",
  58. Age: "25",
  59. Address: "C-Scheme",
  60. City: "Mumbai",
  61. Salary: "500000",
  62. Department: "IT",
  63. },
  64. {
  65. Id: "6",
  66. Name: "Sid",
  67. Age: "25",
  68. Address: "C-Scheme",
  69. City: "Bangalore",
  70. Salary: "500000",
  71. Department: "IT",
  72. },
  73. {
  74. Id: "7",
  75. Name: "Chavi",
  76. Age: "25",
  77. Address: "C-Scheme",
  78. City: "Noida",
  79. Salary: "500000",
  80. Department: "IT",
  81. },
  82. {
  83. Id: "8",
  84. Name: "Nisha",
  85. Age: "25",
  86. Address: "C-Scheme",
  87. City: "Delhi",
  88. Salary: "500000",
  89. Department: "IT",
  90. }
  91. ]
  92. }
  93. }
  94. render() {
  95. return (
  96. <>
  97. <div class="row" style={{marginTop:"10px"}}>
  98. <div class="col-sm-12 btn btn-info">
  99. Add ag-Grid to ReactJS App
  100. </div>
  101. </div>
  102. <div class="row">
  103. <div class="col-md-12">
  104. <div class="card">
  105. <div class="card-body position-relative">
  106. <div className="ag-theme-alpine" style={{ height: "550px" }}>
  107. <AgGridReact
  108. columnDefs={this.state.columnDefs}
  109. rowData={this.state.rowData}
  110. pagination="true" paginationPageSize="5" floatingFilter="true" >
  111. </AgGridReact>
  112. </div>
  113. </div>
  114. </div>
  115. </div>
  116. </div>
  117. </>
  118. );
  119. }
  120. }
  121. export default Aggriddemo
How To Use Ag-Grid In ReactJS

Summary

In this article, we learned how to use ag-grid in a React.js application.