Display large data in table using AG-Grid

Most of the applications have data representation in tabular format with some 7 to 8 columns. But when we want to show the huge amount of data in table which has a columns more than 30 or N columns, some time we also get the scenario like sub-columns of one column, so write a code for such a table is really pain for developer cause if we go with the traditional HTML table then we have to handle all scenarios manually and it also a time consuming, and again if we want change in some column then again we have to follow so many steps.

Normal HTML tags also render the data slow, So in the article, we are going to see how we can overcome all these problems with AG-Grid.

Prerequisites

This article assumes that you have practical knowledge about React JS and javascript. And working with npm libraries.

Preface

This article is going to be covering how to set-up and implement AG-Grid in reactJS and also how to customize columns.

We will follow the steps below in order :-

  1. Install Ag-Grid for react
  2. Implementation of Ag-Grid.
  3. Display data in table.
  4. Customization for sorting.
  5. Performance benchmark against other libraries.
  6. When to use Ag-Grid.

Check Out What It Takes To Build A Successful App Here

Now let’s start the integration process.

Starting with the Development Process

1. Installation

npm i ag-grid-react

We’ve installed a third party library ag-grid-react in our project. Now we are good at implementing tables.

2. Install dependencies

npm i --save ag-grid-community ag-grid-react react-dom-factories

3. Import grid and styles

import { AgGridReact } from 'ag-grid-react';


import 'ag-grid-community/dist/styles/ag-grid.css';

import 'ag-grid-community/dist/styles/ag-theme-balham.css';

4. Set the grid’s configuration in a component

So first we need to declare the columns, it’s a totally key value pair. We have to declare a column name and field for that and then just assign the key of the JSON object that we want to display in the column. In the below code snippets columnDefs is the definition of the column and rowDefs is JSON data to display in the table.

(Note: Column can be defined in 3 ways, we are using a GridOptions way cause the most feasible way of definition.)

We will take two examples of displaying a normal column and display column with sub-column structure.

A. Normal column definition

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 }

      ]

    }

  }

B. Sub-column structure

Make111, make222 are the sub columns of the Make column.

constructor(props) {

    super(props);


    this.state = {

      columnDefs: [

        {

          headerName: "Make",

          children: [

            {

              headerName: 'make 111',

              field: 'make111'

            },

            {

              headerName: 'make 222',

              field: 'make222',

            },

            {

              headerName: 'make 333',

              field: 'make333',

            }

          ]

        },

        { headerName: "Model", field: "model" },

        { headerName: "Price", field: "price" }


      ],

      rowData: [

        { make: "Toyota", model: "Celica", price: 35000, make111: 'Japan', make222: '#kls3fd', make333: '88987' },

        { make: "Ford", model: "Mondeo", price: 32000, make111: 'USA', make222: '#fgf64', make333: '009234' },

        { make: "Porsche", model: "Boxter", price: 72000, make111: 'Germany', make222: '#jklkm67', make333: '57587' }

      ]

    }

  }

5. Render AgGridReact in the component

Now just simply use the AgGridReact component to render the table, pass the column definition and row data to the component as props.

render() {

    return (

      <div

        className="ag-theme-balham"

        style={{

          height: '500px',

          width: '100%'

        }}

      >

        <AgGridReact

          columnDefs={this.state.columnDefs}

          rowData={this.state.rowData}>

        </AgGridReact>

      </div>

    )

  }

1. Normal column definition

6. Output

2. Sub-column structure

We Worked With Insurance Benefits Startup To Accelerate Their Development

7. Make column sortable

We can also sort the data as per specific column, if we want to sort price columns then we just need to make price column sortable, we can do this by just passing props in the column definition, there so many features for ag-grid can check in documentation.

{ headerName: "Price", field: "price", sortable: true }

AG-Grid Documentation

AG-Grid Demo

Performance benchmark against other libraries

If we look, there are so many libraries like Ag-Grid SlickGrid, dhtmlxSpreadsheet, Handsontable and a lot more. Now the question is why Ag-Grid? Ag-Grid renders data faster as compared to others and also it’s very efficient with large data. Some libraries don’t provide some features like pagination, custom arrangement, etc. But Ag-Grid covers all those features that we required.

Ag-Grid provides so many features like filter, pagination, sorting a data, rearrange the columns by using drag and drop. We can customize the theme of the grid, and it’s also very easy to create our own UI for Ag-Grid. Ag-Grid is very easy to implement and understand. We can also export the data from Ag-Grid to spreadsheet.

When to use Ag-Grid

When we have displayed a large amount of data in a table which has so many rows and columns. Don’t use Ag-Grid if you have just 2 to 4 columns with no sub-column structure and less number of rows this will be a bad programming practice.

Use an Ag-Grid when you have N number of columns with N number of rows or data which have sub-column structure like if we have 1 column name Participent and under that there are sub-columns name, language, country. Can see in the below example image or in the demo link. In an easy way when we have to display a complex data structure is table then use Ag-Grid.

Points to remember:

  • You must declare a column and row definition correctly, if not then data presentation might be wrong.
  • Don’t forget to install dependencies, if you forget to install it will raise errors.
  • Don’t use Ag-Grid for small data this will slow down the rendering as compared to Table tag, It is built for a large data representation.
coma

Conclusion

From the above article, we covered how to integrate the ag-grid-react library into our react application and also cover how to customize the column for sorting the data. When we can use Ag-Grid and when not.

Hire dedicated React JS developers & programmers in best affordable price.

Prasad

Tech Expert

Prasad is a Full-stack developer with around 3+ years of experience. He is an expert in building Python integrated web applications, Creating REST APIs with well designed, testable, and efficient, and optimized code. He is having experience in web technologies like ReactJS, JavaScript, etc. He loves to solve technical problems, learn new technologies, and helping others.

Get in touch for a detailed discussion.

Hear From Our 100+ Customers
coma

Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.

author
ADDIE WOOTTEN
CEO, SMILINGMIND
coma

We had very close go live timeline and MindBowser team got us live a month before.

author
Shaz Khan
CEO, BuyNow WorldWide
coma

They were a very responsive team! Extremely easy to communicate and work with!

author
Kristen M.
Founder & CEO, TotTech
coma

We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.

author
Chacko Thomas
Co-Founder, TEAM8s
coma

Mindbowser is one of the reasons that our app is successful. These guys have been a great team.

author
Dave Dubier
Founder & CEO, MangoMirror
coma

Mindbowser was very helpful with explaining the development process and started quickly on the project.

author
Hieu Le
Executive Director of Product Development, Innovation Lab
coma

The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.

author
Alex Gobel
Co-Founder, Vesica
coma

Mindbowser is professional, efficient and thorough. 

author
MacKenzie R
Consultant at XPRIZE
coma

Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.

author
Laurie Mastrogiani
Founder, S.T.A.R.S of Wellness
coma

MindBowser was great; they listened to us a lot and helped us hone in on the actual idea of the app.” “They had put together fantastic wireframes for us.

author
Bennet Gillogly
Co-Founder, Flat Earth
coma

They're very tech-savvy, yet humble.

author
Uma Nidmarty
CEO, GS Advisorate, Inc.
coma

Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.

author
Katie Taylor
Founder, Child Life On Call
coma

As a founder of a budding start-up, it has been a great experience working with Mindbower Inc under Ayush's leadership for our online digital platform design and development activity.

author
Radhika Kotwal
Founder of Courtyardly
coma

The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!

author
Michael Wright
Chief Executive Officer, SDOH2Health LLC
coma

They are focused, patient and; they are innovative. Please give them a shot if you are looking for someone to partner with, you can go along with Mindbowser.

author
David Cain
CEO, thirty2give
coma

We are a small non-profit on a budget and they were able to deliver their work at our prescribed budgets. Their team always met their objectives and I'm very happy with the end result. Thank you, Mindbowser team!!

author
Bart Mendel
Founder, Mindworks