# Row Change Alerts

Canonical page: https://www.adaptabletools.com/docs/handbook-alerting-row-change

- Row Change Alerts fire when the set of Rows in the Grid changes
- This can be either when:
  - a new Row is added to AG Grid
  - an existing Row is deleted from AG Grid

AdapTable offers a dedicated **Row Change** Alert type for row added and removed events:

- `RowAdded` — fires when a row is added
- `RowRemoved` — fires when a row is removed

Each RowChange definition has 2 characteristics:
- `Type` is set to 'RowChange'
- an optional per-row `Rule` (a standard boolean Expression evaluated on the added or removed row)

- Use **Row Change** for a straightforward alert each time a row is **added** or **removed** (`RowChangeTrigger`), with an optional boolean `Rule` on that row
- For row add/remove tied to **timers**, **durations**, **burst** detection, or richer WHERE-style conditions, use [Observable Alerts](https://www.adaptabletools.com/docs/handbook-alerting-observables/index.md) instead — Row Change is not the same as observable row timers

Row Change Alerts fire once per matching row event.

When defining the Alert in Initial State, the [Scope](https://www.adaptabletools.com/docs/dev-guide-columns-scope/index.md) in the Alert Definition should be set to `All`

### Providing Row Change Alerts in Initial State

Use `Type: 'RowChange'` and `RowChangeTrigger: 'Added'` or `'Removed'`. Set `Scope: { All: true }`.

Omit `Rule` to fire on every matching event, or supply a boolean expression evaluated on that row (e.g. `[language] = "TypeScript"`).

```ts [[1, 2, "Alert"], [1, 3, "AlertDefinitions"], [1, 6, "Type"], [1, 6, "RowChange"], [1, 7, "RowChangeTrigger"], [1, 7, "Added"], [1, 11, "Scope"], [1, 14, "AlertProperties"], [1, 16, "HighlightRow"], [2, 21, "Type"], [2, 22, "RowChangeTrigger"], [2, 22, "Removed"], [2, 24, "Scope"], [2, 27, "Rule"], [2, 28, "AlertProperties"], [2, 30, "JumpToRow"]]
const initialState: InitialState = {
  Alert: {
    AlertDefinitions: [
      {
        Name: 'alert-row-added',
        Type: 'RowChange',
        RowChangeTrigger: 'Added',
        MessageType: 'Success',
        MessageHeader: 'Row added',
        MessageText: 'A new row was added to the grid',
        Scope: {
          All: true,
        },
        AlertProperties: {
          DisplayNotification: true,
          HighlightRow: true,
        },
      },
      {
        Name: 'alert-row-removed',
        Type: 'RowChange',
        RowChangeTrigger: 'Removed',
        MessageType: 'Warning',
        Scope: {
          All: true,
        },
        Rule: '[language] = "JavaScript"',
        AlertProperties: {
          DisplayNotification: true,
          JumpToRow: true,
        },
      },
    ],
  },
};
```

## Added Row Alert

Set `RowChangeTrigger` to `'Added'` to fire each time a new row is added to AG Grid.

- If adding the new row programmatically, use [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md) `addGridData` (see [Managing Row Data](https://www.adaptabletools.com/docs/handbook-managing-grid-data-rows-transaction/index.md))
- **Don't** directly use `applyTransaction` (or `applyTransactionAsync`) in AG Grid's API as it bypasses new row detection

**Example: Alerts: Row Added**

Alerts on Rows being Added to AdapTable

- This demo has a Row Change Alert (`RowChangeTrigger: 'Added'`) which fires when a new row is added to the Grid
- We added a `Highlight Row` Behaviour to easily see which Row was added
- The new Row can be added to the Grid in 2 ways:
  - Clicking a Custom Dashboard button that then invokes the `addGridData` method in [Grid API](https://www.adaptabletools.com/docs/handbook-managing-grid-data/index.md)
  - Providing an [Action Column Command button](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) of `create` which opens the [Create Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md) 

- You can mimic adding a new Row in one of 2 ways - both of which will trigger the Alert:
  - Click the `Add Python Row` [Dashboard Button](https://www.adaptabletools.com/docs/ui-dashboard-buttons/index.md)
  - Click the `Create` Button (which automatically displays an empty Row Form allowing you easily to add the new Row)

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  [template]
  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,
  DataUpdateConfig,
  RowFormColumnContext,
} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export default function createPython(): WebFramework {
  return {
    id: Math.floor(Math.random() * (1000000 - 5000 + 1) + 200000),
    name: 'python',
    description: 'A very popular language.',
    created_at: new Date().toDateString(),
    updated_at: new Date().toDateString(),
    pushed_at: new Date().toDateString(),
    github_stars: 5000,
    language: 'Python',
    open_issues_count: 750,
    license: 'MIT License',
    github_watchers: 6671,
    topics: ['duck typing', 'proxies', 'prerformance', 'solid'],
    has_projects: true,
    has_wiki: true,
    has_pages: false,
    closed_issues_count: 12326,
    open_pr_count: 749,
    closed_pr_count: 1708,
    week_issue_change: 15,
  };
}

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Added Alert',
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Actions',
        controls: [{type: 'command', command: 'create'}],
      },
    ],
  },
  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;
    },
  },
  dashboardOptions: {
    customDashboardButtons: [
      {
        label: 'Add Python Row',
        buttonStyle: {
          tone: 'neutral',
          variant: 'outlined',
        },
        onClick: (
          _button: AdaptableButton<DashboardButtonContext>,
          context: DashboardButtonContext
        ) => {
          const python: WebFramework = createPython();
          const dataUpdateConfig: DataUpdateConfig = {
            runAsync: true,
            addIndex: 0,
          };
          context.adaptableApi.gridApi.addGridData([python], dataUpdateConfig);
        },
      },
    ],
  },
  notificationsOptions: {
    position: 'TopCenter',
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-row-added',
          Type: 'RowChange',
          RowChangeTrigger: 'Added',
          MessageType: 'Success',
          MessageHeader: 'You added a row',
          MessageText: 'Hey! A row was added',
          Scope: {
            All: true,
          },
          AlertProperties: {
            DisplayNotification: true,
            HighlightRow: {
              BackColor: 'var(--ab-color-palette-28)',
              ForeColor: 'var(--ab-color-palette-14)',
            },
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'action',
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'topics',
          ],
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```

### Added Row Conditional Alert

The optional `Rule` on a Row Change Alert is a boolean Expression evaluated on the added (or removed) row. When omitted, every matching row event fires the Alert.

**Example: Alerts: Row Added with Condition**

Row Change Alerts fired when Row Added with a per-row Rule

- This demo contains an Add Row Warning Alert with a `Rule` that limits firing to new Rows where:
  - `Language` is *TypeScript*
  - and `Has Wiki` is *true*
- We have added a [Dashboard Button](https://www.adaptabletools.com/docs/ui-dashboard-buttons/index.md) which opens an [Add Row Form](https://www.adaptabletools.com/docs/handbook-row-form/index.md) to enable the new row to be added

- Click the 'Add New Row' Button in the Dashboard to open an Add Row Form
- Provide any `Name`, select 'TypeScript' for `Language` and check `Has Wiki` and click Save
- The new Row will be added to the Grid and the Alert will fire

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  [template]
  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,
  AdaptableOptions,
  DashboardButtonContext,
} from '@adaptabletools/adaptable';

import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Added Alert with WHERE',
  adaptableStateKey: `${Date.now()}`,
  editOptions: {
    showSelectCellEditor: context => {
      return context.column.columnId === 'language';
    },
  },

  rowFormOptions: {
    setPrimaryKeyValue: context => {
      return {
        ...context.rowData,
        id: context.adaptableApi.gridApi.getRowCount() + 1,
      };
    },
  },
  dashboardOptions: {
    customDashboardButtons: [
      {
        label: 'Add New Row',
        onClick: (
          button: AdaptableButton<DashboardButtonContext>,
          context: DashboardButtonContext
        ) => {
          context.adaptableApi.rowFormApi.displayCreateRowForm();
        },
        buttonStyle: {
          variant: 'raised',
          tone: 'info',
        },
      },
    ],
  },
  initialState: {
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: ['name', 'language', 'license', 'has_wiki'],
        },
      ],
    },
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-row-added-where-language',
          Type: 'RowChange',
          RowChangeTrigger: 'Added',
          MessageType: 'Warning',
          Scope: {All: true},
          Rule: "[language] = 'TypeScript' AND [has_wiki] = TRUE",
          AlertProperties: {
            DisplayNotification: true,
            HighlightRow: {
              BackColor: 'var(--ab-color-palette-14)',
              ForeColor: 'var(--ab-color-palette-13)',
            },
          },
        },
      ],
    },
  },
};
```

## Removed Row Alert

Set `RowChangeTrigger` to `'Removed'` to fire each time a Row is removed from AG Grid.

**Example: Alerts: Row Removed**

Alerts on Rows being Removed from AdapTable

- This example fires a Row Change Alert (`RowChangeTrigger: 'Removed'`) when a row is deleted (which we do via an [Action Column Command](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) `delete` button)
- We provided a custom message for the Alert Notification via the `alertMessageText` property in [Alert Options](https://www.adaptabletools.com/docs/technical-reference-alerting/index.md)

- Click the 'delete' [Action Column Command button](https://www.adaptabletools.com/docs/handbook-action-column-command/index.md) and note the Row is deleted and an Alert is fired

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  [template]
  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 {AdaptableOptions, AlertMessageContext} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Row Deleted Alert',
  notificationsOptions: {
    position: 'BottomCenter',
  },
  actionColumnOptions: {
    actionColumns: [
      {
        columnId: 'action',
        friendlyName: 'Delete',
        controls: [{type: 'command', command: 'delete'}],
      },
    ],
  },
  alertOptions: {
    alertMessageText: (alertMessageContext: AlertMessageContext) => {
      if (
        alertMessageContext.rowDataChangedInfo &&
        alertMessageContext.rowDataChangedInfo.rowTrigger == 'Delete'
      ) {
        const deletedRow = alertMessageContext.rowDataChangedInfo.dataRows[0];
        if (deletedRow) {
          return 'You deleted: ' + deletedRow.name;
        }
      }
      return undefined;
    },
  },
  initialState: {
    Dashboard: {
      ModuleButtons: ['Alert'],
      Tabs: [
        {
          Name: 'Toolbars',
          Toolbars: ['Alert'],
        },
      ],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Alert'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Alert: {
      AlertDefinitions: [
        {
          Name: 'alert-row-deleted',
          Type: 'RowChange',
          RowChangeTrigger: 'Removed',
          AlertProperties: {
            DisplayNotification: true,
          },
          MessageType: 'Info',
          Scope: {
            All: true,
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'language',
            'github_stars',
            'license',
            'created_at',
            'has_wiki',
            'updated_at',
            'topics',
            'action',
          ],
          ColumnPinning: {
            action: 'right',
          },
          Name: 'Standard Layout',
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
