# Listening to Cell and Row Changes

Canonical page: https://www.adaptabletools.com/docs/handbook-managing-grid-data-changed-events

- AdapTable provides 2 Events useful for managing Grid Data Changes
  - Row Changed Event - fires whenever data is loaded, added, updated or removed
  - Cell Changed Event - fires when the contents of a cell changes

## Row Changed Event

AdapTable's Row Changed Event fires whenever **Rows** are loaded, added, updated or deleted in AG Grid.

- This event is **only** fired if the change is made via the [Managing Grid Data](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) functions in AdapTable's [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md)
- Or if a `Create`, `Clone`, `Edit` or `Delete` [Action Column Command](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) is used

The Row Changed payload is in the [Grid Data Technical Reference](https://www.adaptabletools.com/docs/technical-reference-managing-grid-data/index.md#row-changed-event)

**Example: Row Changed Events**

Events triggered when Rows are added or deleted

- This demo listens to the Row Changed Event and outputs a [System Status Message](https://www.adaptabletools.com/docs/handbook-system-status-message/index.md) (each with a different type)

- Click the `Load Initial Grid Data` [Dashboard Button](https://www.adaptabletools.com/docs/ui-dashboard-buttons/index.md) to load data and trigger a Row Change of `Load`
- Click the `Add` [Action Column Command](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) in any Row to add a Row and trigger a Row Change of `Add`
- Click the `Edit` [Action Column Command](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) in any Row to update a Row and trigger a Row Change of `Update`
- Click the `Delete` [Action Column Command](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) in any Row to delete a Row and trigger a Row Change of `Delete`

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableButton,
  AdaptableColumn,
  AdaptableOptions,
  DashboardButtonContext,
  RowFormColumnContext,
} from '@adaptabletools/adaptable';
import {WebFramework, rowData} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Changed Event',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Actions',
        controls: [
          {type: 'command', command: 'create'},
          {type: 'command', command: 'edit'},
          {type: 'command', command: 'delete'},
        ],
      },
    ],
  },
  dashboardOptions: {
    customDashboardButtons: [
      {
        label: 'Load Initial Grid Data',
        onClick: (
          button: AdaptableButton<DashboardButtonContext>,
          context: DashboardButtonContext
        ) => {
          const jsRows: WebFramework[] = rowData.filter(
            (f: WebFramework) => f.language === 'JavaScript'
          );
          context.adaptableApi.gridApi.loadGridData(jsRows);
        },
        buttonStyle: {
          variant: 'raised',
          tone: 'warning',
        },
        disabled: () => false,
      },
    ],
  },
  rowFormOptions: {
    setPrimaryKeyValue: context => {
      return {
        ...context.rowData,
        id: context.adaptableApi.gridApi.getRowCount() + 1,
      };
    },
    includeColumnInRowForm: (rowFormColumnContext: RowFormColumnContext) => {
      const column: AdaptableColumn = rowFormColumnContext.adaptableColumn;
      return column.columnTypes?.includes('actionColumn') ||
        column.columnId == 'id'
        ? false
        : true;
    },
  },

  initialState: {
    Dashboard: {
      PinnedToolbars: ['SystemStatus'],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'action',
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'topics',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/gridOptions.ts`**

```ts
import {GridOptions, themeBalham} from 'ag-grid-enterprise';

import {rowData} from './rowData';
import {columnDefs} from './columnDefs';

export const gridOptions: GridOptions = {
  theme: themeBalham,
  defaultColDef: {
    resizable: true,

    sortable: true,
    editable: true,
    filter: true,
    floatingFilter: true,
  },
  columnDefs: columnDefs,
  // set row data to null as we will get it later
  rowData: null,
  sideBar: true,
  suppressMenuHide: true,
  cellSelection: true,
  statusBar: {
    statusPanels: [
      {statusPanel: 'agTotalRowCountComponent', align: 'left'},
      {statusPanel: 'agFilteredRowCountComponent'},
      {
        key: 'Center Panel',
        statusPanel: 'AdaptableStatusPanel',
        align: 'center',
      },
    ],
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {
  AdaptableMessageType,
  AdaptableReadyInfo,
  RowChangedInfo,
} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.eventApi.on('RowChanged', (info: RowChangedInfo) => {
    let messageType: AdaptableMessageType;
    switch (info.rowDataChange.rowTrigger) {
      case 'Load':
        messageType = 'Info';
        break;
      case 'Add':
        messageType = 'Success';
        break;
      case 'Update':
        messageType = 'Warning';
        break;
      case 'Delete':
        messageType = 'Error';
        break;
      default:
        messageType = 'Info';
    }

    adaptableApi.systemStatusApi.setSystemStatus(
      'Row Change Event: ' + info.rowDataChange.rowTrigger,
      messageType
    );
  });
};
```

----------

## Cell Changed Event

The **Cell Changed Event** fires whenever the contents of any cell changes in AG Grid.

This can be the result of a user cell edit or of ticking data.

The Cell Changed payload is in the [Editing Technical Reference](https://www.adaptabletools.com/docs/technical-reference-editing/index.md#cell-changed-event)

**Example: Cell Changed Event**

Event triggered when Cells are edited

- This demo listens to the Cell Changed Event and outputs a [System Status Message](https://www.adaptabletools.com/docs/handbook-system-status-message/index.md) for the Cell's new value
- Note: We use the [Grid API to select a cell](https://www.adaptabletools.com/docs/handbook-selecting-configuring/index.md) in the `Github Stars` Column

- Apply the [Smart Edit](https://www.adaptabletools.com/docs/handbook-editing-smart-edit/index.md) on the selected cell to update the Cell and trigger a Cell Changed Event

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {
  AdaptableButton,
  AdaptableColumn,
  AdaptableOptions,
  DashboardButtonContext,
  RowFormColumnContext,
} from '@adaptabletools/adaptable';
import {WebFramework, rowData} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Cell Changed Event',
  initialState: {
    Dashboard: {
      PinnedToolbars: ['SmartEdit', 'SystemStatus'],
    },
    Theme: {CurrentTheme: 'dark'},

    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'action',
            'name',
            'language',
            'github_stars',
            'github_watchers',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'topics',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
**`src/onAdaptableReady.ts`**

```ts
import {
  AdaptableReadyInfo,
  CellChangedInfo,
  GridCellRange,
  RowChangedInfo,
} from '@adaptabletools/adaptable';

export const onAdaptableReady = ({adaptableApi}: AdaptableReadyInfo) => {
  adaptableApi.smartEditApi.setSmartEditValue(10);

  const gridCellRange: GridCellRange = {
    columnIds: ['github_stars'],
    rowIndexStart: 5,
    rowIndexEnd: 5,
  };
  adaptableApi.gridApi.selectCellRange(gridCellRange);

  adaptableApi.eventApi.on('CellChanged', (info: CellChangedInfo) => {
    adaptableApi.systemStatusApi.setInfoSystemStatus(
      'Cell Change Event to: ' + info.cellDataChange.newValue
    );
  });
};
```
