Using AG Grid in SPFx - Netwoven

Using AG Grid in SPFx

By Debopoma Chaudhury  |  Published on July 6, 2021

Using AG Grid in SPFx


Having worked on quite a range in SPFx, I’ve some handy tips for dashboards, reports, task list etc. in my other blogs (jQuery Data Table with Both Row and Column Selections, Kendo Grids vs UI Fabric Grids in SPFx). While having extensively worked with UI Fabric grids, Kendo grids, jQuery grids, I still faced a lot of issues. In some, customization was not feasible, in some not all features where available and in some performance was extremely slow. That is where we discovered that AG grid is the most apt for these scenarios where there is both customization and bulk of data or one needs real time data update (like mailboxes, reporting tools).

Sample Application

The below sample has three representations of AG Grid in three tabs. I have shown here the flat grid as I have provided the code snippets for the same. The other tabs represent customized grids and advanced grids with custom columns, grouping and other advanced features.

Using AG Grid in SPFx

Steps to implement AG Grid in SPFx:

Create SPFx application

First, we need to create a basic SPFx application from the command prompt using the command “yo @microsoft/sharepoint” and follow the necessary steps as mentioned in this link

Npm install the necessary package files

"@ag-grid-community/all-modules": "^24.1.0",
    "@ag-grid-community/client-side-row-model": "^24.1.0",
    "@ag-grid-enterprise/all-modules": "^24.1.0",
    "ag-grid-community": "^24.1.0",
    "ag-grid-enterprise": "^24.1.0",
    "ag-grid-react": "^24.1.1",

Connect with libraries

Import the necessary libraries for creating grid as shown below:

import { AgGridReact as AgGridReactAdvanced, AgGridColumn as AgGridColumnAdvanced } from 'ag-grid-react';

Create a flat grid in render function

                        flex: 1,
                        minWidth: 30,
                        resizable: true,
                        headerCheckboxSelection: this.isFirstColumn,
                        headerCheckboxSelectionFilteredOnly: true,
                        checkboxSelection: this.isFirstColumn,
                        loadingRenderer: function (params) {
                            if (params.value !== undefined) {
                                return params.value;
                            } else {
                                return '<img src="">';
                    onBodyScroll={(e) => this.scrollHandler(e, this)}

                    {, index) => {
                        return (

                                lockPosition={column.lockPosition ? column.lockPosition : false}
                                cellRenderer={column.cellRenderer ? column.cellRenderer : null}
                                cellRendererParams={column.cellRendererParams ? column.cellRendererParams : null}
                                maxWidth={column.maxWidth ? column.maxWidth : null}
                                minWidth={column.minWidth ? column.minWidth : null}


The above code snippet is of a flat grid which has large data and data is loaded on reaching scroll end of grid page. The grid needs various related properties and methods which need to be provided to be fully functional. The columns and grid data are passed from the .tsx file properties based on requirement.

Grid methods

The below methods are provided to the grid for implementation of all necessary features


In this function the grid apis are initialized.

 onGridReady = (params) => {
        this.gridApi = params.api;
        this.gridColumnApi = params.columnApi;

In this function the current selected row information is captured for further use (say to update/view detailed data).

onSelectionChanged = (ev) => {
        var selectedRows = this.gridApi.getSelectedRows();


This function is used to capture the selected column information.

 cellFocus = (e) => {

        let col = e.column.getUserProvidedColDef();
            selectedColumn: col.field


This function is used to return the first column of the grid. This will be required if you need to display checkboxes in the first column.

 isFirstColumn = (params) => {
        var displayedColumns = params.columnApi.getAllDisplayedColumns();
        var thisIsFirstColumn = displayedColumns[0] === params.column;
        return thisIsFirstColumn;

This function is used to capture all data for further use when you select multiple rows as shown in this snippet.

 rowSelected = (event) => {
        if (event.node.selected == true && this.state.selectedColumn != "selected" && event.api.getDisplayedRowCount() != event.api.getSelectedRows().length) {
                    selectedItem: [],
        if (event.api.getDisplayedRowCount() == event.api.getSelectedRows().length) {
            this.props.checkBoxSelectionMultiple(event.node.selected, event.api.getSelectedRows());


This function is used to load the next set of data on reaching grid page scroll end.

public scrollHandler(event, target) {
        let bufferHeight = 600; 
        if (
   > bufferHeight
        ) {
            if (this.state.isLoaded) {

                this.setState({ isLoaded: false }, () => {


There are different versions of AG Grid. The basic features are available for free use in AG Grid Community version; however, the advanced features are available in AG Grid Enterprise which is a paid version.

The features with are required by most enterprise applications is paid and hence one needs to buy the paid version of AG Grid.

Benefits over other grids

Despite its constraints, the benefits cannot be ignored. Below are some features that I found extremely outstanding, apart from a range of others.

  • Handles large data with very efficient performance.
  • Provides a fully featured grid with all possible features that are required in enterprise solutions.
  • Coding is short and crisp with functions exposed for all sorts of custom features.


So, what have we really got here? For most cases, we might consider going for UI Fabric Grids for simple dashboards and task lists as it is inbuilt with SPFx. In other cases when one needs an advanced grid with detailed features it is best to choose AG Grid if price is not a limitation. A detailed documentation of AG Grid is available here


Leave a comment

Your email address will not be published. Required fields are marked *

Unravel The Complex
Stay Connected

Subscribe and receive the latest insights

Netwoven Inc. - Microsoft Solutions Partner

Get involved by tagging Netwoven experiences using our official hashtag #UnravelTheComplex