Building CRUD in AG Grid with Angular and NGXS
Tags: howto, angular, aggrid
Published: 2021-09-29 10:52:36

---
title: Building CRUD in AG Grid with Angular and NGXS
published: true
date: 2021-09-15 16:55:51 UTC
tags: HowTo,Angular, aggrid
canonical_url: https://blog.ag-grid.com/building-crud-in-ag-grid-with-angular-and-ngxs/
---

 ![Building CRUD in AG Grid with Angular and NGXS](https://blog.ag-grid.com/content/images/2021/09/NGXS--Blog-1.jpeg)

In this post we will look at how to set up and perform CRUD operations in AG Grid while keeping our row data inside the NGXS store. NGXS focuses on using a more concise syntax and simplifying state management. We hope you'll see how easy it is to integrate AG-Grid with Angular and NGXS!

_This post authored by [Marats Stelihs](https://blog.ag-grid.com/author/marats/)_

We've built a sample to illustrate this - please see it in action below:

![Building CRUD in AG Grid with Angular and NGXS](https://blog.ag-grid.com/content/images/2021/08/blog-giff2.gif)

## Live Demo

The live demo shows how to update row data in NGXS store to perform actions on AG-Grid.

Please see the live demo with source code below:

{% stackblitz angular-ivy-btuqbt view=preview %}

## **Contents**

- [How it works](#how-it-works)
- [Adding rows](#add-rows)
- [Editing cell values](#editing-cells)
- [Updating cell values Programmatically](#updating-cells) 
- [Deleting rows](#deleting-rows)
- [Summary](#summary)

## How it works


So, how does AG Grid interact with NGXS? In short, every state change in AG Grid is communicated to NGXS, which returns an updated state, which causes the grid to update itself automatically.

See the illustration below showing how AG Grid works with NGXS:

![Building CRUD in AG Grid with Angular and NGXS](https://blog.ag-grid.com/content/images/2021/08/image-7.png)

This is why all we need to do after a state change in AG Grid is to notify the NGXS store of that. AG Grid will then automatically update itself with the result of that change coming from the store. This is why in the CRUD operations below we only show the code to relay the changes to the NGXS store. In our sample, we show how to add rows, update rows via API, update rows via cell editing and delete rows.

In our sample we also demonstrate how to use other grid features - click the name of the feature to access the relevant documentation:

- Using the [Context Menu](https://www.ag-grid.com/angular-data-grid/context-menu/) to perform CRUD actions
- Using [Cell Editor](https://www.ag-grid.com/angular-data-grid/component-cell-editor/#simple-cell-editor)  to update grid data
- Using [Value Formatter](https://www.ag-grid.com/angular-data-grid/value-formatters/) to change the appearance of grid cell data

## Adding rows


Let's start by looking at how to create new rows. In our example when you right click a row and click on the Add button in the context menu, we copy the clicked row data and assign it a new row id, passing it to the store with an AddRow operation. Adding unique row id values is crucial - without them the NGXS store will not work correctly because we are working with immutable data.

See this implemented in the code of the context menu item below:

```
      {
        name: '<strong>Add</strong> ' + data.model,
        action: () => {
          let newRow = { ...data, id: this.newIds };
          this.newIds++;
          this.store.dispatch(new AddRow(newRow));
        }
      },
```

## Editing cell values


Double-click the top cell in the Model column to start editing it and enter a new value. This update gets saved to the store and the grid is then refreshed.

In order to save changes after cell editing in AG Grid to NGXS we need to use a Value Setter in the AG Grid Default Column Definitions. For more information on these, see our documentation on [Value Setters](https://www.ag-grid.com/javascript-data-grid/value-setters/) and [Column Definitions](https://www.ag-grid.com/angular-data-grid/column-definitions/).

In the valueSetter code, we get the column name (colDef), row data (data) and updated cell value (newValue). We then create an updated version of the row data and send it to the NGXS store with an UpdateRow action. See this below:

```
  defaultColDef = {
    valueSetter: ({ colDef, data, newValue }) => {
      let updatedRow = { ...data };
      updatedRow[colDef.field] = newValue;
      this.store.dispatch(new UpdateRow(updatedRow));
      return false;
    }
  };
```

## Updating Cell Values Programmatically


Now that we showed how to update row values via a cell edit, how about updating a row programmatically? We've built this logic in the "Increase price" item in the row context menu.

In the code example below you see how clicking this context menu item increases the price for this model. We simply send the specific row id to NGXS store with an UpdatePrice action where the price inside the row data for the specific id will be updated. Please see the code for this below:

```
  {
        name: 'Increase price for ' + data.model,
        action: () => {
          this.store.dispatch(new UpdatePrice(rowId));
        }
      }
```

## Deleting rows


To wrap it up, let's see how we can delete a row from the grid. When we right-click a row and then click the Delete item in the context menu, we get the row id and send it in a RemoveRow action to the NGXS store to delete the row. NGXS deletes the row and AG Grid updates itself.

Please see this in the code below:

```
      {
        name: '<strong>Delete</strong> ' + data.model,
        action: () => {
          this.store.dispatch(new RemoveRow(rowId));
        }
      },
```

## Summary


We hope you find this article helpful when using AG Grid with NGXS in Angular. Now you have another alternative to NgRx or Redux with the added bonus of writing a bit less code. Feel free to fork the example from this blog and modify it according to your needs.

If you would like to try out AG Grid check out our getting started guides ([JS](https://www.ag-grid.com/javascript-getting-started/) / [React](https://www.ag-grid.com/react-getting-started/) / [Angular](https://www.ag-grid.com/angular-getting-started/) / [Vue](https://www.ag-grid.com/vue-getting-started/))

Happy coding!