Hands-on with AG Grid's React Data Grid

Rate this content
Bookmark
This video provides a comprehensive hands-on guide to using AG Grid with React. The speaker starts by showing how to install the AG Grid Community and React dependencies, and how to specify row and column data for the grid. The video covers adding CSS styling using AG Grid's CSS and the Alpine style. It explains how to pull data from a remote source using hooks and React's useState, and how to handle CORS issues when running locally. The speaker then dives into creating a custom filter component in React for AG Grid, detailing the methods isFilterActive, doesFilterPass, getModel, and setModel. A year filter with radio buttons is demonstrated, and the video shows how to apply this filter to the grid. The use of default column definitions to reduce boilerplate code is also discussed. The video touches on AG Grid Enterprise features like exporting data to CSV or Excel, grouping data using the Row Grouping feature, and advanced filtering. The speaker highlights the importance of using the getFilterInstance method and the useImperativeHandle hook to expose custom methods in React components. The video also emphasizes the flexibility of AG Grid in customizing cell renderers and handling large datasets efficiently. The importance of using the grid API for various operations and the benefits of virtualization are also discussed. The video concludes by mentioning the availability of the workshop code on GitHub and encourages viewers to explore more on aggrid.com.

From Author:

Get started with AG Grid React Data Grid with a hands-on tutorial from the core team that will take you through the steps of creating your first grid, including how to configure the grid with simple properties and custom components. AG Grid community edition is completely free to use in commercial applications, so you'll learn a powerful tool that you can immediately add to your projects. You'll also discover how to load data into the grid and different ways to add custom rendering to the grid. By the end of the workshop, you will have created an AG Grid React Data Grid and customized with functional React components.

- Getting started and installing AG Grid

- Configuring sorting, filtering, pagination

- Loading data into the grid

- The grid API

- Using hooks and functional components with AG Grid

- Capabilities of the free community edition of AG Grid

- Customizing the grid with React Components

This workshop has been presented at React Summit 2022, check out the latest edition of this React Conference.

FAQ

In React, `useImperativeHandle` is used to expose functions from a child component to a parent component when using refs. This is particularly useful in custom functional components to expose specific functionalities like methods, which are necessary for lifecycle operations in AG Grid custom filters, such as `isFilterActive` and `doesFilterPass`.

To save and restore filter states in AG Grid, you can use the `getFilterModel` method to retrieve the current filter state and save it (e.g., in a database or state management system). To restore the filter state, use the `setFilterModel` method with the saved filter state. This functionality allows you to maintain user filter preferences across sessions or reloads.

Yes, custom filters in AG Grid can access the entire row data. When implementing the `doesFilterPass` method, the filter instance receives parameters that include `data`, which represents the entire row's data. This allows the filter to perform operations based on multiple columns or complex data structures within the row.

AG Grid offers both community and enterprise versions. The community version includes basic features like sorting, filtering, and editing, which are sufficient for many applications. The enterprise version provides additional features such as row grouping, advanced filtering (set and multi-filter), and enhanced support for large datasets, intended for more complex enterprise-level applications.

To implement a dynamic filter component in AG Grid, define a React component that handles the filter logic and use `useImperativeHandle` to expose necessary filter methods to AG Grid. You can dynamically pass parameters like `filterText` or `criteria` through `filterParams` to customize the filter behavior based on the column or user interactions.

In an AG Grid filter component using React functional components, the `useImperativeHandle` hook is used in conjunction with `forwardRef` to expose custom methods. This setup allows AG Grid to call these methods as part of the grid's operations, such as determining if a filter is active or should be applied.

Sean Landsman
Sean Landsman
147 min
01 Jul, 2022

Comments

Sign in or register to post your comment.

Video Transcription

1. Introduction to AG Grid and React

Short description:

We're going to start off by creating an empty react project using create react app, react app. We'll install the necessary dependencies and open up app.js. We'll then install the aggrid React component and pull in some local data to display in the grid. We'll also specify the columns to show. Stay tuned for the agenda.

On the left-hand side, hopefully you can all see my screen, on the left side I've got the GitHub repo. We're going to use this to track the different chapters, if you like, or the sections of this talk. As we go, I'm going to be coding live. If you feel that you missed something or made a mistake or get a bit left behind, refer to the branch for the section that we're talking about and it will describe the code as we go. For example, in the first section, here's the code for the first thing that I'll introduce as I discuss, and then the second part, and so on. And at the end of it, so when we get to the end of that chapter, if you go to... We're about to start on the second section. So for example, if we're about to start the second section with enterprise features, if you just check out that branch, you'll be straight in line, back where we were. But as you go, if you want to follow along, just make sure that you've got the right branch for the section that we're talking about and the code's there. And you'll be able to copy and paste. Alternatively, you can code along as you like or you can just watch, you don't have to code at all and when you, after the workshop, take your time and go look at the GitHub repo and the YouTube videos that we have, and you can follow along in your own time.

So we're going to start off. We're going to create an empty react project using create react app, react app. We're just going to call it hello. Keep an eye on the Discord channel. So we're just a simple boilerplate react application here. It's nothing to do with the grid so far. Just installing all the dependencies. It's taking a time, I'm sorry. Wow, that took longer than I thought, sorry guys and Googles. Okay, so if we go to hello, we'll see a nor project. I'm gonna open this project up in my IDE. And as I said, so far, this is just a straightforward boilerplate React project. There's nothing specific about the AG grid yet. This was a lot snappier before, I apologize for the slight delay here. While that's having a thing, we'll go back to the terminal.

So to get started with AG grid and React, there are two dependencies you need to install. The first one is, we're gonna save it and save the dependencies. First one we're gonna install is AGgridCommunity. AGgridCommunity has all the core features and all the free features. You're able to use the features in AGgrid community without a license. You don't need to contact us. You can use us in commercial projects, and then that contains the core code. Then we also gonna install AGgridReact. AGgridReact is the React rendering side of things, so you need the two together. The AGgridCommunity is the core grid code, and that's part of the community edition, and AGgridReact you'll need as well for React integration, and that provides the React rendering. I will install those dependencies pretty quick. We've got your IDE. So the first thing we're going to do is open up app.js. Let me see if I can open up just in case people have questions. So if you guys or people on the channel have any problems seeing the screen or understanding me, please do feel free to ping a question and I'll try, and answer it. Okay. So we started off with React Create app provided to us. This is just a boilerplate code if we run this. We run this. Okay. The repo, I'll share on the repo. Someone asked, I'll just reshare that. So on the left-hand side, hopefully, you can see my browser. We see the results of the Create React app. I'll just share it. Okay. That's what Create React app has given us. So the first thing we need to do is install our aggrid React component. We'll pull it in. So we'll pull it in with imports. Aggrid React. We'll get rid of this stuff in the app, which we don't need. And we'll use our aggrid React component. So, so far, this won't do anything for us. Because at the minimum, a grid or table requires rows and columns. So the rows will be the horizontal things you see, and the columns will be the vertical. So let's pull in some local data to take a look at what that might look like. So. Focus. So these each of these fields will be something that we can put out and do in the grid. Row data is just an array of JSON data. You can have as many attributes, properties that you like, and then the properties can be complex. In this example, we're only going to be looking at simple data. So numbers and strings. But they can be of any type and then it will be the grid offers functionality for you to read that type and displays it however you wish. So in this case, we will put in some local data in the latest section and we're going to show you how we can more realistically pull this from a remote source. I would say this is 45,000 and this is 50. And then this one will do BMW. 4 series. So that's our row data. We haven't done anything with it yet, so it's. So that will tell the grid to display that row data. That's only half the story there because we also need to show and choose which columns to pull out. To pull out so your row data can have hundreds of fields that maybe you only want to show one or two. How you pick out what data to show is with the column data. So do that next. That's equal and like row data. This is an array of JSON data and at a minimum, you need to specify the field again to show we're going to have Make Model. And price. And again, we need to tell the grid. Good question. What's the agenda. So we've gone straight into it all, pause in a second and Tell you what the agenda is. Okay, so that looks all messed up.

2. Grid Features and Enterprise Functionality

Short description:

We'll cover the basic functionality of the grid, including sourcing, filtering, creating cell renderers, and filters. We'll also touch on enterprise functionality and how to include it. The majority of the workshop will focus on community features and basic usage with React. The code for each section is available on the GitHub repo.

And that's fine because we'll get to that in a minute. So I'll pause for a second to discuss the agenda. What we're going to cover today I mentioned that briefly. But for those that have joined afterwards, I'm going to be describing some of the basic functionality provided by the grid. Sourcing filtering creating cell renderers filters and so on. I'll introduce you to a little bit of the enterprise functionality and how you'd include that. We don't talk a lot about the enterprise features. But if you're something you're interested in and you want to use that in a project will describe how you get a license, how you can try it and briefly, some of the enterprise features, but the the bulk of the talk. What this workshop will be free to use. Community features and just basic stuff really get you going up, get you up and running and going and you can see how easy it is to use with React. Yeah, that's it in a nutshell, these are five different sections and I'll just repeat briefly for those that join later. There's a GitHub repo and each of the five sections here is on the repo and it shows you the code for that particular section. If you want to copy and paste or if you make a mistake along the way and see what I've done, it'll be on the GitHub repo. I believe that answers that question, Clint.

QnA