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.
This article assumes that you have practical knowledge about React JS and javascript. And working with npm libraries.
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 :-
Now let’s start the integration process.
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.
npm i --save ag-grid-community ag-grid-react react-dom-factories
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';
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' } ] } }
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
2. Sub-column structure
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 }
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 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.
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.
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 the latest updates by sharing your email.
Flexible Engagement Model | Secure & Scalable Apps | First Time Right Process
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
We had very close go live timeline and MindBowser team got us live a month before.
They were a very responsive team! Extremely easy to communicate and work with!
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Mindbowser was very helpful with explaining the development process and started quickly on the project.
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.
Mindbowser is professional, efficient and thorough.
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
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.
They're very tech-savvy, yet humble.
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
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.
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
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.
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!!