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.

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.

1. 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 }

      ]

    }

  }

2. 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

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.

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.

We Keep Sharing Tips And Learnings On Growth And Technology. Join Our Popular Newsletter That Goes Out Once In 2 Weeks

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.

What’s on your mind? Tell us a little bit about yourself and your question, and we will be in touch with you within 12 hours

Free eBook on Telemedicine Platform Development: All About Telemedicine

Download Free eBook Now!