How to use ag-grid with angular 14

Viewed 20

I'm trying to setup ag-grid. I wanted to test with some sample data but I'm getting error related to ClientSideRowModelModule.

These are the versions i've installed:

  "dependencies": {
    "@ag-grid-community/angular": "^28.1.1",
    "@ag-grid-community/client-side-row-model": "^28.1.1",
    "@ag-grid-community/core": "^28.1.1",
    "@ag-grid-enterprise/all-modules": "^27.3.0",
    "ag-grid-angular": "^28.1.1",
    "ag-grid-community": "^28.1.1",
    ...

This is my template:

<ag-grid-angular #agGrid style="height: 500px;" class="ag-theme-alpine"
[rowData]="rowData"
[defaultColDef]="defaultColDef"
[modules]="modules"  <----------------- the problem is here
[columnDefs]="[]"
[ensureDomOrder]="true"
[pagination]="true"
(rowDataChanged)="onGridReady($event)"
[pagination]="true"
[paginationPageSize]="paginationPageSize"
[enableBrowserTooltips]="true">
</ag-grid-angular>

This is my logic file:

import { Component, ViewChild } from '@angular/core';
import { AgGridAngular } from 'ag-grid-angular';
import { Module } from 'ag-grid-community';
import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model';

interface Run {
  name: string;
  groupId: number;
}

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent {

  modules: Module[] = [ClientSideRowModelModule]; // Error

  defaultColDef = {
    flex: 1,
    resizable: true,
    sortable: true,
    minWidth: 100,
    filter: true,
    filterParams: {
      buttons: ['cancel', 'reset', 'apply'],
      closeOnApply: true,
      inRangeInclusive: true,
      suppressAndOrCondition: true,
    },
  };

  @ViewChild('agGrid') agGrid: AgGridAngular;

  rowData: Run[];
  paginationPageSize = 20;

  onGridReady(_event: any) {
    this.agGrid.columnApi.autoSizeAllColumns(true);
  }

  constructor() {
    this.rowData.push({ name: 'tanzeel', groupId: 100 });
  }
}

The error is very lengthy:

Error: src/app/app.component.ts:29:24 - error TS2322: Type 'import("C:/Users/TanzeelMirza/Documents/angular14/i.../node_modules/@ag-grid-community/core/dist/cjs/es5/interfaces/iModule").Module' is not assignable to type 'import("C:/Users/TanzeelMirza/Documents/angular14/.../node_modules/ag-grid-community/dist/lib/interfaces/iModule").Module'. Types of property 'rowModels' are incompatible. Type '{ [name: string]: new () => import("C:/Users/TanzeelMirza/Documents/angular14/.../node_modules/@ag-grid-community/core/dist/cjs/es5/interfaces/iRowModel").IRowModel; } | undefined' is not assignable to type '{ [name: string]: new () => import("C:/Users/TanzeelMirza/Documents/angular14/.../node_modules/ag-grid-community/dist/lib/interfaces/iRowModel").IRowModel; } | undefined'. ...mmunity/core/dist/cjs/es5/gridApi").GridApi' is not assignable to type 'import("C:/Users/TanzeelMirza/Documents/angular14/.../node_modules/ag-grid-community/dist/lib/gridApi").GridApi'. Types have separate declarations of a private property 'immutableService'.

29 modules: Module[] = [ClientSideRowModelModule];

Please point out my mistake. I read this documentation also.

1 Answers

first off, you are mixing packages and modules. you have to pick one. Packages - for a one-stop shop. Modules - for reduced size.

if you import all the modules, you might as well just go for packages for simplicity.

This is what you should have in dependencies for modules to have a basic grid setup:

  • @ag-grid-community/core
  • @ag-grid-community/client-side-row-model
  • @ag-grid-community/angular
  • @ag-grid-community/styles

Link to AG Grid Packages vs Modules docs: https://www.ag-grid.com/angular-data-grid/packages-modules/

Link to Angular AG Grid Modules docs: https://www.ag-grid.com/angular-data-grid/modules/

I noticed another issue.

You are mixing grid versions- you should always stick to the same major releases. so in your case, V27 or V28 is your pick.

Regarding module registration.

Try importing the module registry

import { ModuleRegistry } from '@ag-grid-community/core';

and register the modules you want to use there. Perhaps do it in the modules.ts.

ModuleRegistry.registerModules([ClientSideRowModelModule]);

I don't think you need to add modules to the template.

Btw, in the AG Grid documentation examples, you can switch to module examples! Please see the image below in the top right corner.

enter image description here

Link to docs: https://www.ag-grid.com/angular-data-grid/

Related